尝试添加使用 FTP 删除文件夹以及该文件夹中包含的所有子文件夹和文件的功能。
我已经建立了一个递归函数来这样做,我觉得逻辑是正确的,但仍然不起作用。
我做了一些测试,如果路径只是一个空文件夹或只是一个文件,我可以在第一次运行时删除,但如果它是包含一个文件的文件夹或包含一个空子文件夹的文件夹,则无法删除。因此,遍历文件夹并使用删除功能似乎是一个问题。
有任何想法吗?
function ftpDelete($directory)
{
if(empty($directory))//Validate that a directory was sent, otherwise will delete ALL files/folders
return json_encode(false);
else{
global $conn_id;
# here we attempt to delete the file/directory
if( !(@ftp_rmdir($conn_id,$directory) || @ftp_delete($conn_id,$directory)) )
{
# if the attempt to delete fails, get the file listing
$filelist = @ftp_nlist($conn_id, $directory);
# loop through the file list and recursively delete the FILE in the list
foreach($filelist as $file)
ftpDelete($file);
#if the file list is empty, delete the DIRECTORY we passed
ftpDelete($directory);
}
else
return json_encode(true);
}
};