4

我正在尝试使用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));
?>
4

3 回答 3

8

相对路径根据请求的当前工作目录进行解析。根据最初收到请求的 php 文件,这将因请求而异。你需要做的就是把它变成一个绝对路径。

为此,您可以使用多种技术。魔术常数是更有用的__DIR__常数之一,但对于您的问题,因为您知道它相对于文档根目录的路径,您可以执行此操作。

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

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

__DIR__如果您想解析相对于使用__DIR__常量的路径的路径,这很有用。在早期的 php 版本中,您可以使用它dirname(__FILE__)来获取相同的值。

于 2013-08-20T16:38:44.337 回答
3

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

于 2013-08-20T16:38:21.307 回答
0

简单的解决方案

该问题与错误路径有关,但通常是由于站点的缓存而启动的。

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

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

cd www/app/cache rm -fR *

然后缓存得到了新的真实基本路径。

于 2014-11-13T01:18:45.880 回答