文件修改时间“警告 stat 失败”

43

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

我正在尝试创建一个函数,用于删除所有昨天创建的带有“xml”或“xsl”扩展名的文件。 但是每个文件都会出现以下警告:

警告:filemtime() [function.filemtime]:stat 失败 for post_1003463425.xml in /home/u188867248/public_html/ampc/library.php on line 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); 
    }
}

谢谢你的帮助 :)

4个回答

52

我认为问题出在文件的真实路径上。例如,如果您的脚本在'./'上运行,则您的文件位于目录'./xml'内。因此,在获取filemtime或unlink之前最好检查文件是否存在:

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

3
$filelastmodified-time() 应该改为 time()-$filelastmodified,对吗? - legibe
哈哈,我需要这个,通过谷歌搜索找到了它... 这意味着你的代码即使过去两年后仍然能吸引其他人,并且如果代码正确的话,它可能会变得更酷!;-) 顺便说一句,谢谢你! - legibe
5
三年以后仍然帮助人们 :) - pal4life
2
即使过去了4年,仍然如此! - Ömer An
有人在我的代码库中评论了关于filemtime引起错误的问题(实际上是他们忘记构建项目了),我给他们提供了这个答案的链接,以便他们能够看到它是因为文件不存在导致的。 - MrMesees
1
九年后,仍在帮助人们。非常喜欢 SO(Stack Overflow)。 - Christopher Horton

2

在我的情况下,这与路径或文件名无关。如果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']));

这对我很有帮助。

按照天数删除文件的完整代码片段:

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

1
对我来说,涉及到的文件名附加了一个查询字符串,这个函数不喜欢。
$path = 'path/to/my/file.js?v=2'

解决方案是先将其截断:
$path = preg_replace('/\?v=[\d]+$/', '', $path);
$fileTime = filemtime($path);

0

对于喜欢简短代码的人来说,这是一个更短的版本:

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

    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接