【问题标题】:filemtime "warning stat failed for"filemtime“警告统计失败”
【发布时间】:2012-11-03 09:03:46
【问题描述】:

我已经阅读了很多关于它的问题和答案,但我仍然无法解决我的问题......

我正在尝试创建一个函数来删除一天前创建的所有扩展名为“xml”或“xsl”的文件。但是我在我拥有的每个文件上都收到了这个警告:

警告:filemtime() [function.filemtime]: stat failed for post_1003463425.xml in /home/u188867248/public_html/ampc/library.php 在第 44 行

该目录下所有文件的结构名称为“post_ + randomNum + .xml”(例如:post_1003463425.xml 或 post_1456463425.xsl)。所以我认为这不是一个编码问题(就像我在其他问题中看到的那样)。

我的函数代码是这样的:

 function deleteOldFiles(){
    if ($handle = opendir('./xml')) {
        while (false !== ($file = readdir($handle))) { 

            if(preg_match("/^.*\.(xml|xsl)$/i", $file)){

                $filelastmodified = filemtime($file);

                if ( (time()-$filelastmodified ) > 24*3600){
                    unlink($file);
                }
            }
        }
        closedir($handle); 
    }
}

感谢您的帮助:)

【问题讨论】:

    标签: php regex filemtime


    【解决方案1】:

    我认为问题在于文件的真实路径。例如,您的脚本正在处理“./”,您的文件位于“./xml”目录中。因此,在获取 filemtime 或取消链接之前,最好检查文件是否存在:

      function deleteOldFiles(){
        if ($handle = opendir('./xml')) {
            while (false !== ($file = readdir($handle))) { 
    
                if(preg_match("/^.*\.(xml|xsl)$/i", $file)){
                  $fpath = 'xml/'.$file;
                  if (file_exists($fpath)) {
                    $filelastmodified = filemtime($fpath);
    
                    if ( (time() - $filelastmodified ) > 24*3600){
                        unlink($fpath);
                    }
                  }
                }
            }
            closedir($handle); 
        }
      }
    

    【讨论】:

    • @somy-a $filelastmodified-time() 应该是 time()-$filelastmodified 否?
    • @somy-a 哈哈,我需要那个,在谷歌搜索后发现了它......这意味着即使在两年后你的代码也会引起其他人的兴趣,如果它可能会更酷代码是对的! ;-) 顺便谢谢你!
    • 即使在 3 年多之后也能帮助人们:)
    • 即使在 4 年后!
    • 有人在我的 repo 上评论 filemtime 导致错误(他们忘记构建项目),将这个答案链接到他们,以便他们可以看到这是因为文件不存在。
    【解决方案2】:

    对我来说,所涉及的文件名附加了一个查询字符串,这个函数不喜欢它。

    $path = 'path/to/my/file.js?v=2'
    

    解决办法是先把它砍掉:

    $path = preg_replace('/\?v=[\d]+$/', '', $path);
    $fileTime = filemtime($path);
    

    【讨论】:

      【解决方案3】:

      在我的情况下,它与路径或文件名无关。如果 filemtime()、fileatime() 或 filectime() 不起作用,请尝试 stat()。

      $filedate = date_create(date("Y-m-d", filectime($file)));
      

      变成

      $stat = stat($directory.$file);
      $filedate = date_create(date("Y-m-d", $stat['ctime']));
      

      这对我有用。

      完成按天数删除文件的sn-p:

      $directory = $_SERVER['DOCUMENT_ROOT'].'/directory/';
      $files = array_slice(scandir($directory), 2);
      foreach($files as $file)
      {
          $extension      = substr($file, -3, 3); 
          if ($extension == 'jpg') // in case you only want specific files deleted
          {
              $stat = stat($directory.$file);
              $filedate = date_create(date("Y-m-d", $stat['ctime']));
              $today = date_create(date("Y-m-d"));
              $days = date_diff($filedate, $today, true);
              if ($days->days > 1) 
              { 
                  unlink($directory.$file);
              }
          } 
      }
      

      【讨论】:

        【解决方案4】:

        喜欢短代码的人的短版本:

        // usage: deleteOldFiles("./xml", "xml,xsl", 24 * 3600)
        
        
        function deleteOldFiles($dir, $patterns = "*", int $timeout = 3600) {
        
            // $dir is directory, $patterns is file types e.g. "txt,xls", $timeout is max age
        
            foreach (glob($dir."/*"."{{$patterns}}",GLOB_BRACE) as $f) { 
        
                if (is_writable($f) && filemtime($f) < (time() - $timeout))
                    unlink($f);
        
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 2013-11-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-30
          • 2020-10-20
          相关资源
          最近更新 更多