One of my blog user “Chris” asked for FTP clean up script, so here it is:


//CONFIG SECTION
//*******************************************************
// Credentials for FTP Server
$source_server_ip = "your_domain_or_IP"; // Server IP or domain name eg: 212.122.3.77 or ftp.domain.tld
// Credentials for FTP account
$ftphost = "ip_or_hostname_of_ftp"; // FTP host IP or domain name
$ftpacct = "userid"; // FTP account
$ftppass = "password"; // FTP password
$logs_dir = "/"; //FTP Remote Folder
$email_notify = 'your_email@domain.com'; // Email address for backup notification
$backupexpireindays=21; //3 weeks expire time in days, 21 days = 7*24*60
//END OF CONFIG SECTION
//*******************************************************


//Do not edit below this line
$backupexpireindays=($backupexpireindays*24)*3600; //convert it to seconds, 24 hours * 60 minutes * 60 seconds

// Delete any other backup with filetime greater than expire time, before create new backup
$conn_id = ftp_connect($ftphost);
$login_result = ftp_login($conn_id, $ftpacct, $ftppass);

ftp_chdir($conn_id, $logs_dir);
$files = ftp_nlist($conn_id, ".");
foreach ($files as $filename) {
        $fileCreationTime = ftp_mdtm($conn_id, $filename);
        //$date = date("F j, Y, g:i a", ftp_mdtm($conn_id, $filename));
        //print "
Timestamp of '$filename': $date"; $fileAge=time(); $fileAge=$fileAge-$fileCreationTime; if ($fileAge > $backupexpireindays) { // Is the file older than the given time span? //echo "
The file $filename is older than Expire time :$expiretime ...Deleting\n"; ftp_delete($conn_id, $filename); //echo "
Deleted

"; } } ftp_close($conn_id); print "Remote FTP clean up Finish deleted files older than $backupexpireindays days"; ?>

Enjoy !

  1. Chris says:

    Babar –

    This works great! Thank you so much.. I’ve been googling for days to find a simple clean script to preform this function.

    Much appreciated.

    Best,
    Chris