Skip to content

Instantly share code, notes, and snippets.

@rajvanshipradeep15
Created May 16, 2013 12:32
Show Gist options
  • Save rajvanshipradeep15/5591392 to your computer and use it in GitHub Desktop.
Save rajvanshipradeep15/5591392 to your computer and use it in GitHub Desktop.
php: delete file if exists more than 3 hrs
<?php
//Delete file if it exists for more than 3 hrs
$path = $_SERVER['DOCUMENT_ROOT']."/delete_test_files";
if ($handle = opendir($path)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && preg_match('/.*.ps/i', $entry)) {
$last_modified = filemtime($path .'/' . $entry);
$last_modified_str = date("Y-m-d H:i:s", $last_modified);
$from_time = strtotime($last_modified_str);
$to_time = strtotime(date('Y-m-d H:i:s'));
$time_diff = ((round(abs($to_time - $from_time)) / 60))/60;
echo $time_diff;
if ($time_diff >= 3) {
unlink($path . '/' . $entry);
}
}
}
closedir($handle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment