How to delete all files in a directory on FTP with PHP

5.18K viewsFTPPHP

I have a PHP script that connects to a FTP server, but how can I delete all files in the directory?

Laura Unselected an answer November 1, 2020
Add a Comment
Ludo (anonymous) 0 Comments

 // directory to delete
$directory = '/var/www/html/directory123/';
 // ftp connection
$connection = ftp_connect("host");
ftp_login($connection, "user", "password");
 // change the directory
ftp_chdir($connection, $directory);
 // list all files
$files = ftp_nlist($connection, ".");
foreach ($files as $file)
 // delete all files in directory
 ftp_delete($connection, $file);

Laura Edited answer November 1, 2020
Add a Comment
You are viewing 1 out of 3 answers, click here to view all answers.
Write your answer.