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

Jim*_*med 4 php ftp rmdir

尝试添加使用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);
    }
};
Run Code Online (Sandbox Code Playgroud)

Fil*_*efp 5

我花了一些时间通过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);
  }
}
Run Code Online (Sandbox Code Playgroud)