Friday, October 10, 2008

Remove files using file created time using PHP

/*
Author : Evergreenphp
Date : 10-10-2008
Desc : This is will delete all the 'xml' files inside the $path_dir folder
not only that but also this code checks whether the file created before 10 mins
or else that files will not be deleted

h - hour, i - minutes, s - seconds, m - month, d - date of the month, Y - Year
*/

/* Configuration setting starts */
$path_dir = "/www/htdocs/phpdocs/missing_man/general";
$cdate = date("Y-m-d h:i:s",mktime(date("h"), date("i")-10, date("s"), date("m"), date("d"), date("Y")));
$file_type = "txt";
/* Configuration setting ends */

/* Function call */
Delete_file_withtime($path_dir, $cdate, $file_type);


/* Function Definition starts */
function Delete_file_withtime($path_dir, $cdate, $file_type){

$dh = opendir($path_dir);
while (($file = readdir($dh)) !== false) {
$ext_test = explode(".", $file);
echo "
".$date_access = date("Y-m-d h:i:s", fileatime($file));
$stcon_dt = strtotime($date_access);
$encon_dt = strtotime($cdate);

if($file != "." && $file != ".." && $ext_test[1] == $file_type) {
if($encon_dt > $stcon_dt){
//@unlink($file);
echo "success";
}
}

}
}
/* Function Definition ends */
?>

No comments:

Popular Posts