Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filemtime not finding files

Tags:

php

filemtime

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));
?>
like image 502
AndroidDev Avatar asked Dec 20 '22 01:12

AndroidDev


1 Answers

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.

like image 61
Orangepill Avatar answered Jan 07 '23 18:01

Orangepill