Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?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&lt;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