Delete folder and all files on FTP connection

后端 未结 4 1149
春和景丽
春和景丽 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:23

    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.

提交回复
热议问题