Select Page

PHP Script that deletes files within a remote directory via FTP older than a given amount of days

The following PHP script lets you connect to a remote location via PHP’s FTP functionality and delete all files older than a certain amount of given days in that directory, it also provides functionality to list files that should be ignored in a array, these files listed in the array won’t be deleted even if they are older than the given days.

This script can be used together with a cron job, to automate a process. For example if you have a directory where you store automated backups via FTP and don’t have the time to delete old backups, this script can help to automate the process given that you only have FTP access to the directory 

$host = "backups.mydomain.com"; //Replace with your host
 
$username = "myUsername"; //Replace with your username
 
$password = "myPassword"; //Replace with your password
 
$mode = "passive"; //Leave blank to go to active mode
 
$dir = "/"; //Put the name of the directory in here where you want to loop through files, put / for root directory
 
$daysOld = '2'; //Enter the age of files in days, if a file should be deleted that's older than 2 days enter 2
 
$filesToSkip = array('.','..','.ftpquota'); //Contains the files that the script needs to skip if it comes accross them
 
$notificationEmail = '[email protected]'; //The email address to send a notification when file fails to delete
 
//FTP session starting
 
$connection = ftp_connect($host);
 
$login = ftp_login($connection,$username,$password);
     
 
if(!$connection || !$login){
 
    die('Connection attempt failed!');
 
}
 
 
if($mode == 'passive'){
 
    //Switching to passive mode
 
    ftp_pasv($connection,TRUE);
 
}else{
 
    ftp_pasv($connection,FALSE);
 
}
 
 
//Calcuting the datetime of todays day minus the amount of 2days entered
 
$dateToCompare = date('Y-m-d',  strtotime('-'.$daysOld.' days',time()));
 
 
//Looping through the contents of the provided directory
 
 $files = ftp_nlist($connection,$dir); //ftp_rawlist — Returns a detailed list of files in the given directory
 
 foreach($files as $file){
 
     //Check if the file is in the list of files to skip, if it is we continue the loop
 
     if(in_array($file, $filesToSkip)){
 
         continue;
 
     }
 
 
     $modTime = ftp_mdtm($connection, $file);
 
     if(strtotime($dateToCompare) >= $modTime){
 
         if(!ftp_delete($connection,$file)){ //Deleting the file that needs to be deleted
 
             //If the file fails to delete we send a mail to the administrator
 
             mail($notificationEmail, 'FAILED TO DELETE FILE', 'FAILED TO DELETE FILE: '.$file);
 
         }
 
     }
 
 }

Related Articles

Last Updated On March 22, 2018

2 Comments

  1. Md arif hossain

    Thanks for your code.
    Is it possible to review again your code.
    Date $dateToCompare it’s work but unable to file delete.
    FAILED TO DELETE FILE.

    Thanks again for share.

    Reply
  2. Md. arif hossain

    Hallo Thanks again for your code.

    Just change this code then work fine.

    ftp_chdir($connection, $dir);
    $files = ftp_nlist($connection, “.”);

    Reply

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Special Offers