filemtime找不到文件

时间:2013-08-20 16:21:22

标签: php filemtime

我正在尝试使用filemtime获取上次修改某些文件的日期和时间。不幸的是,这对我的所有文件都失败了。我尝试过使用完整的完整路径(例如www.mysite.com/images/file.png)。我尝试过只使用扩展名(例如/images/file.png),还只使用文件名本身(file.png)。什么都行不通。如何传递路径引用以便它可以找到我的文件?谢谢!

<br />n<b>Warning</b>:  filemtime() [<a href='function.filemtime'>function.filemtime</a>]: stat failed for www.mysite.com/images/hills.png in <b>D:\Hosting\11347607\html\mobile\updateImages.php</b> on line <b>20</b><br 

这是php脚本:

<?php

    include '../scripts/connection.php';

    //get information on this location from the database
    $sql = "SELECT * FROM types";
    $result = mysql_query($sql);

    $array = array();
    while ($row = mysql_fetch_array($result)) {
        $path = $row['path'] . $row['picture_url'];
        $last_mod = filemtime($path);
        $this_file = array(
            'filename' => $row['picture_url'],
            'last_mod' => $last_mod
        );
        $array[$row['type']] = $this_file;
    }

    echo json_encode(array('result'=>'success', 'message'=>$array));
?>

3 个答案:

答案 0 :(得分:6)

相对路径根据请求的current working directory进行解析。根据最初收到请求的php文件,这将从请求更改为请求。你需要做的是把它变成一条绝对的道路。

您可以使用几种技术。 __DIR__魔术常量是更有用的但是对于你的问题,因为你知道它相对于文档根的路径你可以做到这一点。

$last_mod = filemtime($_SERVER["DOCUMENT_ROOT"]."/images/file.png");

这使用您的Web服务器的文档根目录为您提供了解决路径的起点(文件系统路径等同于http://mysite.com URL)。

如果要解析相对于使用__DIR__常量的路径的路径,

__DIR__非常有用。在早期的php版本中,您可以使用dirname(__FILE__)来获取相同的值。

答案 1 :(得分:2)

你的$path是网址吗? www.mysite.com/images/hills.png?它应该是文件而不是URL。 http://php.net/manual/en/function.filemtime.php

答案 2 :(得分:0)

简易解决方案

问题与错误路径有关,但通常会因网站缓存而启动。

因此,简单的解决方案是删除缓存,然后将在您的服务器中再次创建错误的基本路径。

根据我的经验,我遇到了symfony 2'缓存的问题。 简单快捷的解决方案是删除app / cache目录。

cd www / app / cache rm -fR *

然后缓存获得了新的真实基址。

相关问题