Skip to main content

Posts

Showing posts with the label delete old files in cron

Delete images older than one year in php

 The following code deletes images that contain the word "snapshot" and are older than one year. <?php $imagePattern = "/snapshot/i"; $directory = "./images"; if (($handle = opendir($directory)) != false) {     while (($file = readdir($handle)) != false) {         $filename = "$directory/$file";         //echo $filename." ";                 if ( filemtime($filename) <=strtotime("-1 year") && preg_match($imagePattern, $filename)) {             unlink($filename);             //$filetime=filemtime($filename);                         //echo $filename."  ".date('l dS \o\f F Y h:i...