4

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));
?>
AndroidDev
  • 20,466
  • 42
  • 148
  • 239
  • It's recommended to use an absolute path as the base, so that you're always sure the canonical file name is what you expect it to be. – Ja͢ck Aug 20 '13 at 16:23
  • So did you check what `$path` is before you call filemtime()? did you check if that path really does exist from something other than PHP? If it does exist, then check if whatever user PHP's running as has writes to access that directory tree + file. – Marc B Aug 20 '13 at 16:28
  • I tried that first, but got the same result. I'm going to edit my post to show the error message when using that approach. – AndroidDev Aug 20 '13 at 16:28
  • Try echoing the $path inside the loop and post the path to hills.png you see on the output and the original path to hills.png on your disk – Jithesh Aug 20 '13 at 16:28
  • @Jithesh: The path echos correctly. However, you mention 'my disk' which makes me think that it's important to point out that I'm using the path on the server (e.g. www.mysite.com/images/hills.png) because that is the version that I want to compare to. Is it a problem to use the server version instead of the disk version? – AndroidDev Aug 20 '13 at 16:40

3 Answers3

8

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.

Orangepill
  • 24,500
  • 3
  • 42
  • 63
3

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

Jigar
  • 3,256
  • 1
  • 30
  • 51
  • Yes, it is a URL. I need to compare the version of the file that I have on my android phone with the version stored on the server. If the server version is newer, then I re-download it. I don't necessarily care what version of the file is stored on my disk, until such time as I upload that to the server. It's the server version that I am using as the master version. – AndroidDev Aug 20 '13 at 16:42
  • Sorry to say but you cannot find the modified time of the files on the server through `filemtime` – Jigar Aug 20 '13 at 16:44
  • If you have control over the server you can create a web service to return the last modified time of the requested file. – Orangepill Aug 20 '13 at 16:46
  • Yes, @Orangepill is right. There are many ways actually to fetch modified time. If you have control over that server, A simple php file can fetch you modified time, OR php ftp fucntion `ftp_mdtm` http://www.php.net/manual/en/function.ftp-mdtm.php can also get you a modified time. – Jigar Aug 20 '13 at 17:01
0

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.