Delete folder and all files on FTP connection

老子叫甜甜 提交于 2019-12-17 20:58:13

问题


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

回答1:


I took some time to write my own version of a recursive delete function over ftp, this one should be fully functional (I tested it myself).

Try it out and modify it to fit your needs, if it's still not working there are other problems. Have you checked the permissions on the files you are trying to delete?

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



回答2:


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



回答3:


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



回答4:


You have to check (using ftp_chdir) for every "file" you get from ftp_nlist to check if it is a directory:

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

    ftpDelete($file)

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

This easy trick will work, because if ftp_chdir works, the current $file is actually a folder, and you've moved into it. Then you call ftpDelete recursively, to let it delete the files in that folder. Afterwards, you move back (ftp_cdup) to continue.



来源:https://stackoverflow.com/questions/8513605/delete-folder-and-all-files-on-ftp-connection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!