【问题标题】:Getting last modified info from files with PHP when filemtime, stat['mtime'], and get_headers fail当 filemtime、stat['mtime'] 和 get_headers 失败时,使用 PHP 从文件中获取最后修改的信息
【发布时间】:2010-08-04 19:37:10
【问题描述】:

我正在尝试以与上次修改时间相反的顺序显示图像。不幸的是, get_headers() 似乎只适用于 url,而 stat['mtime'] 和 filemtime() 对我来说都失败了。我还有其他方法可以获取文件的最后修改信息吗?这是我目前的代码:

if (isset($_GET['start']) && "true" === $_GET['start'])
{
    $images = array();

    if ($dir = dir('images'))
    {
        $count = 0;

        while(false !== ($file = $dir->read()))
        {
            if (!is_dir($file) && $file !== '.' && $file !== '..' && (substr($file, -3) === 'jpg' || substr($file, -3) === 'png' || substr($file, -3) === 'gif'))
            {
                $lastModified = filemtime($file);
                $images[$lastModified] = $file;
                ++$count;
            }
        }

        echo json_encode($images);
    }
    else { echo "Could not open directory"; }
}

【问题讨论】:

  • $count 变量是我之前成功的测试的残余,看看我是否至少可以检索文件名。

标签: php last-modified


【解决方案1】:

您应该在调用filemtime($file) 之前添加文件名的路径。试试

$lastMod = filemtime("images/".$file);

【讨论】:

  • 太棒了。像魅力一样工作!
猜你喜欢
  • 1970-01-01
  • 2011-10-30
  • 1970-01-01
  • 2014-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多