Created
August 9, 2018 11:00
-
-
Save ZiTAL/7f4b6ae52f5fb31bf725bba3d051d54c to your computer and use it in GitHub Desktop.
php: delete old files by date
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
$dirs = array | |
( | |
'/net/localhost/biltegia/web-server/uploads', | |
'/net/localhost/biltegia/web-server/photos/uploads', | |
'/net/localhost/biltegia/web-server/photos/parts', | |
'/net/localhost/biltegia/web-server/photos/tmp' | |
); | |
$time = 1 * 60 * 60 * 24 * 7 * 4; // hilabete bat - 1 month | |
$tmp = array(); | |
foreach($dirs as $dir) | |
{ | |
$rs = array(); | |
scandirr($dir, array('.', '..'), $rs); | |
foreach($rs as $r) | |
$tmp[] = $r; | |
} | |
$rs = $tmp; | |
$now = time(); | |
foreach($rs as $r) | |
{ | |
$t = filemtime($r); | |
if($now-$time>$t) | |
$tmp[] = $r; | |
} | |
$rs = $tmp; | |
foreach($rs as $r) | |
{ | |
$c = "shell_exec(\"rm -rf {$r}\")"; | |
shell_exec("rm -rf \"{$r}\""); | |
} | |
// all files in first array | |
function scandirr($folder, $deny = array(), &$result) | |
{ | |
if(is_dir($folder)) | |
{ | |
$resources = scandir($folder); | |
foreach($resources as $resource) | |
{ | |
if(!in_array($resource, $deny)) | |
{ | |
$r = $folder."/".$resource; | |
if(is_dir($r)) | |
scandirr($r, $deny, $result); | |
else | |
$result[] = $r; | |
} | |
} | |
ksort($result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment