Reputation: 21237
I'm trying to get the date and time of the last modification of some files using filemtime
. Unfortunately, this is failing for all of my files. I've tried using the full, complete path (e.g. www.mysite.com/images/file.png). I've tried just using the extension (e.g. /images/file.png) and also just the file name itself (file.png). Nothing is working. How can I pass the path reference so that it can find my files? Thanks!
<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
This is the php script:
<?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));
?>
Upvotes: 4
Views: 9528
Reputation: 1
Easy Solution
The problem is related to the bad path, but often that is launched due to the site's cache.
So, the easy solution is to remove the cache, then the bad base path will be created again in your server.
In my experience, i had got that problem with symfony 2' cache. And the easy quick solution was to remove the app/cache directory.
cd www/app/cache rm -fR *
Then the cache got the new true basepath.
Upvotes: 0
Reputation: 24655
Relative paths resolve base on the current working directory of the request. This will change from request to request depending on what php file initially received the request. What you need to do is to make this into a absolute path.
There are several techniques that you can use for this. The __DIR__
magic constant is one of the more useful but for your problem, since you know what the path it relative to the document root you can do this.
$last_mod = filemtime($_SERVER["DOCUMENT_ROOT"]."/images/file.png");
This uses your web server's document root to give you a starting point to resolve your path against (the file system path equivelent to the http://mysite.com url).
__DIR__
is useful if you want to resolve a path relative to the path that the __DIR__
constant is used in. On earlier php versions you can use dirname(__FILE__)
to get the same value.
Upvotes: 8
Reputation: 3322
Is your $path
a url ? www.mysite.com/images/hills.png
? it should be file instead of URL. http://php.net/manual/en/function.filemtime.php
Upvotes: 3