Created
February 18, 2010 09:02
-
-
Save masuidrive/307497 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// sweeper.php - public domain | |
// まずは試しに ..../sweeper.php?debug=1で実行してください | |
$sweep_dir = "/tmp/data/"; // 消したいディレクトリ ex) "/tmp/data/" 最後の/は必須です | |
$expire_at = 30; // ファイルを保存する日数を設定します | |
$debug = array_key_exists('debug', $_GET); | |
date_default_timezone_set('Asia/Tokyo'); | |
$num = sweep_files($sweep_dir, $expire_at ,$debug); | |
echo "Purged $num files"; | |
function sweep_files($sweep_dir, $expire_at, $debug) { | |
$count = 0; | |
if(!($dir = @opendir($sweep_dir))){ die("can't open $sweep_dir");} | |
while($file = readdir($dir)) { | |
if($file=='.' || $file=='..') { | |
// NOOP | |
} | |
else if(is_dir($file)) { | |
$count += sweep_files($sweep_dir . $file . '/', $expire_at, $debug); | |
} | |
else { | |
$name = $sweep_dir . $file; | |
$mtime = filemtime($name); | |
if((time() - $mtime) >= 60*60*24*$expire_at) { | |
if(file_exists($name)) { | |
if($debug) { | |
$mtime_ymd = date("Y/m/d H:i:s", $mtime); | |
echo "will purge> ".$mtime_ymd." $name<br/>"; | |
} | |
else { | |
unlink($name); | |
} | |
$count++; | |
} | |
} | |
} | |
} // end while | |
closedir($dir); | |
return $count; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment