cancel
Showing results for 
Search instead for 
Did you mean: 
Go to solution

PHP & filemtime

I am having some troubles accessing file stats via PHP, in particular I need to use filemtime to order a somewhat long list of files in a directory. This is what I've been attempting:

<?php
require_once("config.inc.php");
$image_dir = IMAGE_PATH . "/carousel";
$thumbs_dir = IMAGE_PATH . "/carousel/thumbs";

$handle = opendir($thumbs_dir);

if ($handle) {
    $files = array();
    // I load an array with all the files
    while (false !== ($entry = readdir($handle))) {
        if ($entry !== "." && $entry !== "..") {
            $files[] = $entry;
        }
    }

    usort($files, function($a, $b) {
        return filemtime($a) < filemtime($b);
    });

    // override with browser-digestible URLs
    $image_dir = "../images/uploads/carousel";
    $thumbs_dir = "../images/uploads/carousel/thumbs";
    ?>
    <select class="image-picker" name="image_select[]" id="image_select" multiple="multiple"> 
    <?php
    foreach ($files as $entry) {
// other logic here, unimportant as the warning occurs earlier

and this is what I'm getting in the error_log

[11-Jun-2019 19:04:23 UTC] PHP Warning:  filemtime(): stat failed for namefile-x.xyz

any clue why is this happening? Is filemtime somewhat limited?

5 REPLIES 5
Highlighted
Super User II
Super User II
Solution

Re: PHP & filemtime

@alphacharlie 

I did a quick Google Search and the issue is the way PHP is looking at the path.

 

See https://stackoverflow.com/questions/13386082/filemtime-warning-stat-failed-for for reference and you should be good to go

 

 

GoDaddy Pro User - Managing over 300 WP Sites

Re: PHP & filemtime

I was over that topic too, it doesn't solve my problem: I've tried with both absolute and relative paths to no avail. Also, with a mini-script where I point a specific failing file (and using relative paths), filemtime works perfectly. It basically fails in the loop

Super User II
Super User II
Solution

Re: PHP & filemtime

I'm not familiar enough with filemtime - but everything you've done looks like it should work

 

Thoughts of things to try (some you may have done already)

 

1) "namefile-x.xyz" - did you replace the actual name or is that the error - is there a file in the folder is can't read??

 

2) can you output / test filemtime($a) & filemtime($b)

to make sure it is giving you what you are expecting

 

Right now that is the only other thing I can recommend

 

GoDaddy Pro User - Managing over 300 WP Sites

Re: PHP & filemtime

disregard: you've opened my eyes! Thank you.

Solution

Re: PHP & filemtime

copying and pasting, I'd overlooked the fact that $entry was just pointing to a filename without a complete path (either absolute or relative). I made sure to pass the whole path inside the while loop and it's working like a charm

$image_dir = "../images/uploads/carousel";
$thumbs_dir = "../images/uploads/carousel/thumbs";

$handle = opendir($thumbs_dir);

if ($handle) {
    $files = array();
    // I load an array with all the files
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            $files[filemtime($thumbs_dir."/".$entry)] = $entry;
        }
    }

    ksort($files);

Thank you again: I would have stared at those 10 lines of code for another 2 hours without noticing the macro-error