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

5.18K viewsFTPPHP
0

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
1
Ludo (anonymous) 0 Comments

<?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
Add a Comment
0
Anonymous 0 Comments

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
Add a Comment
Write your answer.