php filemtime“警告统计失败”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13386082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:18:06  来源:igfitidea点击:

filemtime "warning stat failed for"

phpregexfilemtime

提问by Gerard Brull

I already read it so many questions and answers about it but I can't still solve my problem...

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

I'm trying to create a function that deletes all the files with "xml" or "xsl" extension that has been created one day ago. But I'm getting this warning on each file I have:

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

Warning: filemtime() [function.filemtime]: stat failed for post_1003463425.xml in /home/u188867248/public_html/ampc/library.php on line 44

警告:filemtime() [function.filemtime]:第 44 行 /home/u188867248/public_html/ampc/library.php 中 post_1003463425.xml 的统计失败

All the files of this directory have the same structure name "post_ + randomNum + .xml" (example: post_1003463425.xml or post_1456463425.xsl). So I think it's not an encoded problem (like I saw in other questions).

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

The code of my function is this:

我的函数代码是这样的:

 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); 
    }
}

Thanks for your help :)

谢谢你的帮助 :)

回答by Somy A

I think the problem is the realpath of the file. For example your script is working on './', your file is inside the directory './xml'. So better check if the file exists or not, before you get filemtime or unlink it:

我认为问题在于文件的真实路径。例如,您的脚本正在处理“./”,您的文件位于“./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); 
    }
  }

回答by Andrew

For me the filename involved was appended with a querystring, which this function didn't like.

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

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

Solution was to chop that off first:

解决方案是先把它砍掉:

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

回答by lufc

Shorter version for those who like short code:

对于那些喜欢短代码的人来说更短的版本:

// 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);

    }

}