You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FTP删除目标目录内全部内容(保留目录)的技术实现问询

How to Delete All Files in /public_html/ via FTP (Keep the Directory Intact)

Alright, let's tackle this problem: we need to wipe every file and subdirectory inside /public_html/ but leave the /public_html/ directory itself standing. Here's how to complete and improve that ftpDelete function you started with:

First, let's outline the core logic we need:

  • Grab all items in the target directory
  • Skip the . and .. special directory markers (we don't want infinite loops or messing with parent folders)
  • For each item:
    • If it's a file: Delete it directly
    • If it's a subdirectory: Recursively clear its contents first, then delete the empty subdirectory
  • Never touch the original /public_html/ directory itself

Here's the completed, commented function:

function ftpDelete($conn, $directory) {
    echo "<pre><b>FTP Files on Server:</b>\n";
    // Fetch all items in the target directory
    $filelist = ftp_nlist($conn, $directory);
    
    foreach($filelist as $file) {
        // Skip current/parent directory placeholders
        if ($file === "." || $file === "..") {
            continue;
        }

        // Build a full path to avoid relative path confusion
        $fullPath = rtrim($directory, '/') . '/' . $file;

        // Check if the item is a directory (most servers return -1 for directory sizes)
        if (ftp_size($conn, $fullPath) === -1) {
            // Recursively empty the subdirectory first
            ftpDelete($conn, $fullPath);
            // Delete the now-empty subdirectory
            if (ftp_rmdir($conn, $fullPath)) {
                echo "Deleted empty directory: $fullPath\n";
            } else {
                echo "Failed to delete directory: $fullPath\n";
            }
        } else {
            // It's a file—delete it
            if (ftp_delete($conn, $fullPath)) {
                echo "Deleted file: $fullPath\n";
            } else {
                echo "Failed to delete file: $fullPath\n";
            }
        }
    }
    echo "</pre>";
}

// Example usage (uncomment and fill in your details):
// $ftpConn = ftp_connect('your-ftp-host.com') or die('Could not connect to FTP server');
// ftp_login($ftpConn, 'your-username', 'your-password');
// ftpDelete($ftpConn, '/public_html/');
// ftp_close($ftpConn);

Important Tips:

  • Permissions Check: Make sure your FTP user has write/delete access to every file and subdirectory inside /public_html/—otherwise some operations will fail.
  • Recursive Safety: This function only deletes content inside the directory you pass it. It will never delete the directory itself, so /public_html/ stays intact by design.
  • Alternative Directory Detection: If ftp_size() doesn't work for your server (some return non--1 values for directories), use ftp_rawlist() to parse file permissions and confirm if an item is a directory.
  • Test First: Run this on a dummy directory before production to avoid accidental data loss!

内容的提问来源于stack exchange,提问作者colinreedy674

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:38:10