我正在尝试使用 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/