Delete folder and all files on FTP connection

后端 未结 4 1146
春和景丽
春和景丽 2020-12-11 08:29

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 f

4条回答
  •  感情败类
    2020-12-11 09:11

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

提交回复
热议问题