Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP + FTP delete files in folder

I just wrote a PHP Script which should connect to FTP and delete all files in a special folder.

It looks like this, but I have no clue what command I need to delete all files in the folder logs.

Any idea?

<?php

// set up the settings
$ftp_server = 'something.net';
$ftpuser = 'username';
$ftppass = 'pass';

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftpuser, $ftppass);

// delete all files in the folder logs
????????

// close the connection
ftp_close($conn_id);

?>
like image 248
Lupo Avatar asked Nov 01 '10 13:11

Lupo


People also ask

How do I delete a file using FTP in PHP?

The ftp_delete() function deletes a file on the FTP server.

How do I delete a file in PHP?

The unlink() function deletes a file.


2 Answers

// Delete all files in the folder logs
$logs_dir = "";
ftp_chdir($conn_id, $logs_dir);
$files = ftp_nlist($conn_id, ".");
foreach ($files as $file)
{
    ftp_delete($conn_id, $file);
}    

You might want to do some checking for directories, but at a basic level, that is it.

like image 107
Reese Moore Avatar answered Sep 30 '22 05:09

Reese Moore


The PHP manual on the FTP functions has the answers.

  • ftp_nlist() to list
  • ftp_delete() to delete

the user contributed notes give full examples for a "delete folder" function. (Handle with care.)

like image 31
Pekka Avatar answered Sep 30 '22 05:09

Pekka