Jump to content

Deleteing files from server after x amount of days


graham23s

Recommended Posts

Hi Guys,

 

using this code i delete files older than 45 days on my server:

 

<?php
     error_reporting(0);
     
     $number_of_days = 45;
     // using UNIX timestamp in the table.../////////////////////////////////////////////
      
     $stamp = strtotime("-".$number_of_days." day");  
     
     // using MySQL DATETIME...//////////////////////////////////////////////////////////
     $stamp = date('Y-m-d H:i:s', strtotime("-".$number_of_days." day"));
     
     $result = $result = mysql_query("SELECT * FROM `uploaded_files` WHERE `date_added` <= '$stamp'");
     while ($row = mysql_fetch_array($result)) {

     $file = $row['doc_file'];

     unlink("docs/$file");

     // remove files...//////////////////////////////////////////////////////////////////
     }     
     mysql_query("DELETE FROM `uploaded_files` WHERE `date_added ` <= '$stamp'");  
     
     // if everything went ok.../////////////////////////////////////////////////////////
     if($result) {
     
        echo'<br /><b>Thank You, All Files Older Than 45 Days Have Been Deleted!</b><br /><br />';
     
     } else {
     
        echo '<br /><b><font color="red">Error:</font> Sorry There Was An Unexpected Error Purging The System!</b><br /><br />';
     
     }
?>

 

This works great it deletes the actual files on the server after 45 days , but theres also information associated with the files in mysql aswell like comments made when uploaded, the id , date uploaded etc

 

i really  need that deleted aswell , along with the files what would be the best way to alter the code to do this?

 

thanks for any help

 

Graham

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.