Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Created August 9, 2018 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZiTAL/7f4b6ae52f5fb31bf725bba3d051d54c to your computer and use it in GitHub Desktop.
Save ZiTAL/7f4b6ae52f5fb31bf725bba3d051d54c to your computer and use it in GitHub Desktop.
php: delete old files by date
$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