php如何自动清理图片资源?本文主要介绍了php实现图片自动清理的方法,可实现清除固定日期内没有访问的图片。希望对大家有所帮助。
具体实现方法如下:
<?php
/**
* 图片清理计划程序,删除文件下两周没有访问的文件
*/
$srootpath = dirname(__file__);
//define(time_line ,"-7 day");
//删除几天没有访问图片的时间
$dir = $srootpath .directory_separator.'upload';
$itimeline = strtotime("-7 day");
//$itimeline = time();
$shanddate = date("ymd");
$slogdir = dirname(__file__).directory_separator.'imglog';
$slog = $slogdir.directory_separator.$shanddate.'.txt';
if(!file_exists($slogdir)) mkdir($slogdir, 0777,true);
_clearfile($dir , $itimeline, $slog);
$send = 'at'."\\t" .date("y-m-d h:i:s")."\\t".'exec over'."\\n";
echo $send;
error_log($send, 3, $slog);
/**
* 清除文件操作,传入需要清除文件的路径
* @param unknown_type $spath
*/
function _clearfile($spath, $itimeline, $slog){
if(is_dir($spath)){
$fp = opendir($spath);
while(!false == ($fn = readdir($fp))){
if($fn == '.' || $fn =='..') continue;
$sfilepath = $spath.directory_separator.$fn;
_clearfile($sfilepath ,$itimeline, $slog);
}
}else{
if($spath != '.' && $spath != '..'){
//. ..文件直接跳过,不处理
$ilastview = fileatime($spath);
if($ilastview < $itimeline){
if(@unlink($spath) === true){
//echo date("y-m-d h:i:s").'成功删除文件'.$spath;
//file_put_contents($slog,'success del file :'.$spath."\\n", file_append);
//exit;
$str =date("y-m-d h:i:s")."\\t".'success del file :'.'['.$spath.']'."\\n";
error_log($str, 3, $slog);
//exit;
}
}
}
}
}
?>
相关推荐:
php 文件锁定写入
php 文件本身操作
php 文件夹操作
以上就是php自动清理图片资源的详细内容。