php - filemtime 找不到文件

标签 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));
?>

最佳答案

相对路径基于 current working directory 解析的请求。这将根据最初收到请求的 php 文件而随请求而变化。你需要做的是把它变成绝对路径。

您可以使用多种技术来实现这一点。 __DIR__ 魔法常量是比较有用的常量之一,但对于您的问题而言,因为您知道它相对于文档根目录的路径,所以您可以执行此操作。

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

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

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

关于php - filemtime 找不到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18340169/

相关文章:

使用 +mtime 的 Linux Find 命令给出不正确的结果

php/mysql 显示表 1 和表 2 中的列

php - 从 Android 应用程序在 PHP 文件上发布 JSON 文件,并将 JSON 解码为 PHP 上的局部变量

php - Apache 内存问题

php - 获取文件夹中最近更新文件的文件时间

windows - ctime, mtime, holding directory, windows, linux

php - MySQL 在两个日期之间选择未按预期工作

php - 我如何判断我的 Apache2+PHP 是否使用 CGI/FCGI?

php - FTP:我可以覆盖文件并保持其修改日期吗?