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
<?php // 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
There is no PHP function to delete all files in a directory.
You can list all files with ftp_nlist().
Use a loop to delete all files with ftp_delete().
Laura Changed status to publish October 26, 2020