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:s A', $filetime)."
";
        }
       
    }
    echo "Successfully deleted old images";
    closedir($handle);
}


?>

Comments

0 Responses to "Delete images older than one year in php"