php-ftp-client icon indicating copy to clipboard operation
php-ftp-client copied to clipboard

Recursive deletion of directories

Open skurrilewelt opened this issue 1 year ago • 6 comments

Tried to remove a directory structure like this:

-root --content ----file1 ----file2 ----subdir ------file4 ------file3

and the method FtpClient::removeDir(directory) doesn't jumps in to the subdir and as a result the directory content will not be deleted as intended.

Change line 373 in mentioned class to: $this->removeDir($fileInfo['path']); will solve this issue. Or miss I something?

skurrilewelt avatar Aug 26 '24 14:08 skurrilewelt

Hey, thanks for opening this issue.

print_r($this->isDir("root/content/subdir"));

Check this and see what result you will get, it must be true, otherwise we will use another method listDirDetails to see details about the file root/content/subdir and guess what the problem with it!

AmraniCh avatar Aug 26 '24 14:08 AmraniCh

I think, it is obvious. You can't call rmdir because it won't delete directories which are not empty.

skurrilewelt avatar Aug 30 '24 06:08 skurrilewelt

The removeDir method should delete everything within a directory whether it's a file or a directory.

AmraniCh avatar Aug 30 '24 08:08 AmraniCh

Don't know, why you closed this without looking into your code:

public function removeDir(string $directory) : bool
    {
        $list = array_reverse($this->listDirDetails($directory, true));
        foreach ($list as $fileInfo) {
            if ($fileInfo['type'] === 'file') {
                $this->wrapper->delete($fileInfo['path']);
                continue;
            }
            $this->wrapper->rmdir($fileInfo['path']);
        }

        return $this->wrapper->rmdir($directory);
    }

There is the recursive call to removeDir missing, in fact you call rmdir which won't works

skurrilewelt avatar Oct 03 '24 15:10 skurrilewelt

Hi @skurrilewelt

I closed the issue because of no response from your end, and I explained that the removeDir should recursively remove all the files and directories, and you have not provided any additional information about your issues like the directory structure or the error message.

Please share the output of this command:

 $list = array_reverse($this->listDirDetails($directory, true));
print($list);

You have to know that some FTP servers may have different implementations of the FTP specification and its extensions, so we are not expecting our library methods to work perfectly with all FTP servers.

If you share additional info we can guess what your FTP server is doing differently to update our code and finally fix the issue.

thanks for your understanding!

AmraniCh avatar Oct 03 '24 19:10 AmraniCh

Hi @AmraniCh

I had the same Problem an i analysed it. I have installed the filezilla Server on an Windows Server -root --subfolder named rmm ---subsubfolder named ratgeber <- This one i want to delete

Here is the debug log: <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx [Trace] Session 0x1e4cd950f10 with ID 161162 created. <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx [Response] 220-FileZilla Server 1.5.1 <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx [Response] 220-Please visit https://filezilla-project.org/ <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx [Response] 220-welcome to MAXFIVE ftp! <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx [Response] 220 we wish an perfect day! <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx [Command] USER myFTPUser <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx [Response] 331 Please, specify the password. <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx [Command] PASS **** <20.05.2025 08:47:00> File-based Authenticator [Trace] authenticate: is_from_system: 0, impersonation_token: { username: "", home: "" } <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx myFTPUser [Response] 230 Login successful. <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx myFTPUser [Command] PWD <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx myFTPUser [Response] 257 "/" is current directory. <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx myFTPUser [Command] CWD rmm/ratgeber <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx myFTPUser/TVFS/local_filesys [Trace] info(I:\ftproot\myFTPUser\rmm\ratgeber): result: 0 <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx myFTPUser [Response] 250 CWD command successful <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx myFTPUser [Command] CWD / <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx myFTPUser/TVFS/local_filesys [Trace] info(I:\ftproot\myFTPUser): result: 0 <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx myFTPUser [Response] 250 CWD command successful <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx myFTPUser [Command] TYPE A <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx myFTPUser [Response] 200 Type set to A <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx myFTPUser [Command] PORT 172.20.2.xx,216,102 <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx myFTPUser [Response] 200 PORT command successful. <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx myFTPUser [Command] LIST -R /rmm/ratgeber/ <20.05.2025 08:47:00> FTP Session 161162 172.20.2.xx myFTPUser [Response] 550 Invalid file name or path <20.05.2025 08:47:00> FTP Server [Status] Session 161162 ended gracefully.

after i tried to call your function: echo var_dump($client->listDirDetails('rmm/ratgeber', true)); because you call this also in your class on row 367 and i got the exeption error "Invalid file name or path" after i removed the "true" for recursive $client->listDirDetails('rmm/ratgeber') and everything works well. this is my input for you, hope this helps!! cheers robert

speedy-129 avatar May 20 '25 07:05 speedy-129