【问题标题】:Delete all images older than 1 hour删除所有超过 1 小时的图像
【发布时间】:2014-01-18 13:48:19
【问题描述】:

我想从名为 tempimages 的文件夹中删除所有超过 1 小时的图像。 我在 Stack Overflow 上找到了以下示例,但出现解析错误:

语法错误,第 13 行 /delete-old-images.php 中出现意外的“{”

<?php  
function destroy($dir) {
$mydir = opendir($dir);
while($file = readdir($mydir)) {
    if($file != "." && $file != "..") {
        chmod($dir.$file, 0777);
        if(is_dir($dir.$file)) {
            chdir('.');
            while($dir.$file) {
                if(date("U",filectime($file) >= time() - 3600)
                {
                    unlink($dir.$file)
                }
            }

        }
        else
            unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");
    }
}
closedir($mydir);
}

destroy("tempimages/");
?>

我的服务器数据:

PHP Version 5.3.18-nmm1
System  Linux #116-Ubuntu SMP Tue Nov 12 19:37:57 UTC 2013 x86_64 
Build Date  Oct 26 2012 16:30:11 
Server API  Apache 2.0 Handler

如何解决?

【问题讨论】:

    标签: php


    【解决方案1】:

    您忘记关闭date()

     if(date("U",filectime($file)) >= time() - 3600)
                          -------^
    

    这里漏了一个分号

    unlink($dir.$file);
                 -----^
    

    【讨论】:

      【解决方案2】:

      你还没有关闭 if 条件的括号。

      if(date("U",filectime($file) >= time() - 3600)
      

      应该是

      if(date("U",filectime($file) >= time() - 3600))
      

      错过了分号

      unlink($dir.$file)
      

      应该是

      unlink($dir.$file);
      

      【讨论】:

        猜你喜欢
        • 2015-07-14
        • 1970-01-01
        • 2017-04-21
        • 1970-01-01
        • 1970-01-01
        • 2016-10-03
        • 2017-03-16
        • 2022-06-16
        • 2019-03-08
        相关资源
        最近更新 更多