当前位置:首页 > PHP教程 > PHP高级教程

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程序设计有所帮助。


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!