删除FTP连接上的文件夹及其所有文件

4

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

你要删除的目录或其子目录中是否包含空格? - Filip Roséen - refp
文件或文件夹不要有空格,只使用字母字符。 - JimmyJammed
你所涉及的目录的文件系统权限是否允许你删除这些目录? - sarnold
我创建了一个问题,请查看:https://stackoverflow.com/questions/70144584/how-can-i-use-this-function - emanuel
我在这个话题上创建了一个问题,请看:https://stackoverflow.com/questions/70144584/how-can-i-use-this-function - emn
我在这个主题上创建了一个问题,请查看链接 - emn
4个回答

6

我花了一些时间编写了自己的递归删除ftp函数,这个函数应该是完全可用的(我亲自测试过)。

试着运行它并进行修改以适应你的需求,如果还是不能工作,可能存在其他问题。你检查过要删除的文件的权限了吗?

function ftp_rdel ($handle, $path) {

  if (@ftp_delete ($handle, $path) === false) {

    if ($children = @ftp_nlist ($handle, $path)) {
      foreach ($children as $p)
        ftp_rdel ($handle,  $p);
    }

    @ftp_rmdir ($handle, $path);
  }
}

没错,这个可以用。我使用的是相对路径,但绝对路径也应该可以。 - vladkras
如何创建FTP连接句柄?以下代码是否正确:$conn_id = ftp_connect($ftp_server); $handle = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); - Mostafa
1
以下解决方案对我来说效果更好:https://dev59.com/Q1HTa4cB1Zd3GeqPSYy-#4069590 - 这个答案缺少 ftp_chdir - Akira Yamamoto

1
function recursiveDelete($handle, $directory)
{   echo $handle;
    # here we attempt to delete the file/directory
    if( !(@ftp_rmdir($handle, $directory) || @ftp_delete($handle, $directory)) )
    {            
        # if the attempt to delete fails, get the file listing
        $filelist = @ftp_nlist($handle, $directory);
        // var_dump($filelist);exit;
        # loop through the file list and recursively delete the FILE in the list
        foreach($filelist as $file) {            
            recursiveDelete($handle, $file);            
        }
        recursiveDelete($handle, $directory);
    }
}

1

好的,我找到了问题所在。由于我没有进入我要删除的精确目录,每个递归调用的文件路径都不是绝对的:

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);
};

0

您需要使用 ftp_chdir 检查从 ftp_nlist 获取的每个“文件”,以确定它是否是一个目录:

foreach($filelist as $file)
{
    $inDir = @ftp_chdir($conn_id, $file);

    ftpDelete($file)

    if ($inDir) @ftp_cdup($conn_id);
}

这个简单的技巧可行,因为如果ftp_chdir工作正常,则当前的$file实际上是一个文件夹,并且您已经进入了该文件夹。然后,您递归调用ftpDelete,让它删除该文件夹中的文件。之后,您返回(ftp_cdup)以继续其他操作。


这也是我刚想到的想法。然而,这仍然不起作用。我进行了一些测试,所以如果路径只是一个空文件夹或一个文件,我可以在第一次运行时删除,但如果它是包含一个文件或一个空子文件夹的文件夹,则无法删除。感到困惑。 - JimmyJammed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接