当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP实现图片自动清理的方法

PHP实现图片自动清理的方法

2018年05月15日  | 移动技术网IT编程  | 我要评论

超级杂役,训诫文集中营,bt核工厂千涩

本文实例讲述了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程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网