andre1990 Posted September 16, 2011 Share Posted September 16, 2011 Hi guys, Im a newb here, so bear with me! Still learning the ropes. I want to delete all files from my server that are 6 hours old since being uploaded to the site. I believe i need a cron job? I currently have this script in its framework... <?php //time interval for deletion to occur... $x = 30; //timestamp $current_time = time(); //the file you wish to delete $file_name = 'file.txt'; //timestamp $file_creation_time = filemtime($filename); //extract difference $difference = $current_time - $file_creation_time; //if difference = $x...then delete file if ($difference == $x) { unlink($file_name); } Would that work automatically or would it need a cron to run it? And how could i test it, that bit worries me. Many thanks. Link to comment https://forums.phpfreaks.com/topic/247269-delete-files-after-x-amount-of-time/ Share on other sites More sharing options...
web_craftsman Posted September 16, 2011 Share Posted September 16, 2011 nothing works automatically if is is not said to work Yes, your code may be run fron cron-task Or you may imitate simple cron by checking time of your customer's visits. Not $difference == $x, but $difference >= $x Link to comment https://forums.phpfreaks.com/topic/247269-delete-files-after-x-amount-of-time/#findComment-1269917 Share on other sites More sharing options...
andre1990 Posted September 16, 2011 Author Share Posted September 16, 2011 Im running on CentOS, would the Cronjob look like this? #<minute> <hour> <day> <month> <dow> <command> /* 4 * * * /link/to/command/php/file Would that run every 4 hours? Link to comment https://forums.phpfreaks.com/topic/247269-delete-files-after-x-amount-of-time/#findComment-1269921 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.