删除FTP连接中的文件夹和所有文件 [英] Delete folder and all files on FTP connection
问题描述
我已经构建了一个递归函数来完成此操作,并且我觉得这样的逻辑是正确的,但仍然无法正常工作。
我做了一些测试,如果路径只是一个空文件夹,只是一个文件,但如果它是包含一个文件的文件夹或包含一个空子文件夹的文件夹,则无法删除。所以它似乎是遍历文件夹并使用函数删除的问题。
有什么想法?
function ftpDelete($ directory)
{
if(empty($ directory))//验证目录是否被发送,否则将删除ALL文件/文件夹
返回json_encode(false);
else {
global $ conn_id;
#这里我们尝试删除文件/目录
if(!(@ ftp_rmdir($ conn_id,$ directory)|| @ftp_delete($ conn_id,$ directory)))
{
#如果删除尝试失败,则获取文件列表
$ filelist = @ftp_nlist($ conn_id,$ directory);
#循环遍历文件列表并递归删除列表中的文件
foreach($ filelist as $ file)
ftpDelete($ file);
#如果文件列表为空,请删除我们通过
ftpDelete($ directory)的DIRECTORY;
}
else
return json_encode(true);
}
};
确定发现我的问题。由于我没有进入我想要删除的确切目录,每个被调用的递归文件的路径都不是绝对的:
function ftpDeleteDirectory($ directory)
{
global $ conn_id;
if(empty($ directory))//验证目录是否被发送,否则将删除所有文件/文件夹
返回json_encode(false);
else {
#这里我们尝试删除文件/目录
if(!(@ ftp_rmdir($ conn_id,$ directory)|| @ftp_delete($ conn_id,$ directory)))
{
#如果删除尝试失败,则获取文件列表
$ filelist = @ftp_nlist($ conn_id,$ directory);
#遍历文件列表并递归删除列表中的FILE
foreach($ filelist as $ file)
{
//返回json_encode($ filelist);
ftpDeleteDirectory($ directory。'/'。$ file); / ***这是我必须将绝对路径重新映射到文件的位置*** /
}
#if文件列表为空,删除我们通过
ftpDeleteDirectory($ directory)的DIRECTORY;
}
}
返回json_encode(true);
};
Trying to add the ability to delete a Folder using FTP and all subfolders and files contained within that folder.
I have built a recursive function to do so, and I feel like the logic is right, but still doesnt work.
I did some testing, I am able to delete on first run if the path is just an empty folder or just a file, but can't delete if it is a folder containing one file or a folder containing one empty subfolder. So it seems to be a problem with traversing through the folder(s) and using the function to delete.
Any ideas?
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);
}
};
Ok found my problem. Since I wasn't moving into the exact directory I was trying to delete, the path for each recursive file being called wasn't absolute:
function ftpDeleteDirectory($directory)
{
global $conn_id;
if(empty($directory))//Validate that a directory was sent, otherwise will delete ALL files/folders
return json_encode(false);
else{
# 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)
{
// return json_encode($filelist);
ftpDeleteDirectory($directory.'/'.$file);/***THIS IS WHERE I MUST RESEND ABSOLUTE PATH TO FILE***/
}
#if the file list is empty, delete the DIRECTORY we passed
ftpDeleteDirectory($directory);
}
}
return json_encode(true);
};
这篇关于删除FTP连接中的文件夹和所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!