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

2.20K viewsFTPPHP
0

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

Unselected an answer
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().

Changed status to publish
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);
}

Edited answer
Add a Comment
Write your answer.