phpnoobie9 Posted March 28, 2008 Share Posted March 28, 2008 I have a form for people to submit images. Is it possible to select a directory and delete images after x amount of time? Link to comment https://forums.phpfreaks.com/topic/98344-is-it-possible-to-auto-delete-image-after-a-certain-time/ Share on other sites More sharing options...
cooldude832 Posted March 28, 2008 Share Posted March 28, 2008 yes/no Since it needs to be called via the execution of a php script you lose the ability to make a continous function However you can build something that will autoload via CRON or each page load that will do it look around php.net for how to read the file dates. Link to comment https://forums.phpfreaks.com/topic/98344-is-it-possible-to-auto-delete-image-after-a-certain-time/#findComment-503260 Share on other sites More sharing options...
BlueSkyIS Posted March 28, 2008 Share Posted March 28, 2008 yes, it is possible. i would make a crontab job run every x hours/minutes/days, unlink'ing images older than y hours/minutes/days. Link to comment https://forums.phpfreaks.com/topic/98344-is-it-possible-to-auto-delete-image-after-a-certain-time/#findComment-503261 Share on other sites More sharing options...
discomatt Posted March 28, 2008 Share Posted March 28, 2008 As an alternative if you don't have access to cron Include a snippet/function in you main class contruct or included header file that finds files older than x date and removes them. If this is going to be a process-consuming function, or you expect to have lots of traffic, randomize and create a % chance of happening... or simply track the last time it was cleaned in a flat file, and have the function check against a set time and clean and update the flat file with the current date Let the users activate the 'cron' for you Link to comment https://forums.phpfreaks.com/topic/98344-is-it-possible-to-auto-delete-image-after-a-certain-time/#findComment-503271 Share on other sites More sharing options...
phpnoobie9 Posted March 28, 2008 Author Share Posted March 28, 2008 As an alternative if you don't have access to cron Include a snippet/function in you main class contruct or included header file that finds files older than x date and removes them. If this is going to be a process-consuming function, or you expect to have lots of traffic, randomize and create a % chance of happening... or simply track the last time it was cleaned in a flat file, and have the function check against a set time and clean and update the flat file with the current date Let the users activate the 'cron' for you Where will I start if I want to make a script that deletes images in a certain directory older than x amount of time...as far as using a function? Link to comment https://forums.phpfreaks.com/topic/98344-is-it-possible-to-auto-delete-image-after-a-certain-time/#findComment-503333 Share on other sites More sharing options...
redarrow Posted March 28, 2008 Share Posted March 28, 2008 Delete files within a directory x time......... just adapt the code ok. <?php function cleantmp() { $seconds_old = 3600; $directory = "/var/tmp"; if( !$dirhandle = @opendir($directory) ) return; while( false !== ($filename = readdir($dirhandle)) ) { if( $filename != "." && $filename != ".." ) { $filename = $directory. "/". $filename; if( @filemtime($filename) < (time()-$seconds_old) ) @unlink($filename); } } } ?> Link to comment https://forums.phpfreaks.com/topic/98344-is-it-possible-to-auto-delete-image-after-a-certain-time/#findComment-503336 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.