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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.