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?
Solved! Go to Solution.
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
Once your issue is resolved,
please be sure to come back and click accept for the solution
Get Better Support on the Community Boards!
Etiquette When Asking for Help from the Community
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
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
Once your issue is resolved,
please be sure to come back and click accept for the solution
Get Better Support on the Community Boards!
Etiquette When Asking for Help from the Community
disregard: you've opened my eyes! Thank you.
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