尝试使用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);
}
};