SarahB12 Posted September 5, 2012 Share Posted September 5, 2012 Hey guys, I'm receiving the following error when trying to sort files in a directory: PHP Warning: filemtime() [<a href='function.filemtime'>function.filemtime</a>]: stat failed for Late Fee's.pdf in \index.php on line 137 <?php $dirPath = './live'; $dirFiles = array(); function mtime_sort($a, $b) { if (filemtime($a) == filemtime($b)) return 0; return (filemtime($a) > filemtime($b)) ? -1 : 1; } if ($handle = opendir($dirPath)) { while (false !== ($file = readdir($handle))) { if (eregi("\.pdf",$file) || eregi("\.txt",$file) || eregi("\.jpg",$file)){ $dirFiles[] = $file; } } closedir($handle); } usort($dirFiles, 'mtime_sort'); foreach($dirFiles as $file) { echo "<li><a href='".$file."'>".substr($file, 0, -4)."</a></li>\n"; } ?> This is line 137 if (filemtime($a) == filemtime($b)) . I've tried numerous things, but I'm still running into this error. Any suggestions? :-\ Thanks! ~ SarahB ~ Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 5, 2012 Share Posted September 5, 2012 function filetime_callback($a, $b) { if (filemtime($a) === filemtime($b)) return 0;// always use identical comparison return filemtime($a) < filemtime($b) ? -1 : 1; } // Then sort with usort() usort($files, "filetime_callback"); Quote Link to comment Share on other sites More sharing options...
scootstah Posted September 5, 2012 Share Posted September 5, 2012 It could be a permissions issue. Does it work on other files? Do the files have different permissions set? if (eregi("\.pdf",$file) || eregi("\.txt",$file) || eregi("\.jpg",$file)){ $dirFiles[] = $file; } This is one of the most ridiculous file-type checks I've ever seen. All you are doing is checking that one of those strings exists anywhere in the filename. So, ANicePicture.jpgJustKiddingImAVirus.exe is perfectly valid and passes your check. Aside from the fact that your regex doesn't work, checking the filename doesn't do any good, because the filename is not indicative of the filetype. You need to be checking the MIME type, with something like fileinfo. Quote Link to comment Share on other sites More sharing options...
SarahB12 Posted September 5, 2012 Author Share Posted September 5, 2012 All of the files in the directory are giving me this error. The file-type check is set to only display those file-types in the directory. There are other file-types in the directory, but since it's password protected we shouldn't have a Justkiddingimavirus.exe issue. It could be a permissions issue. Does it work on other files? Do the files have different permissions set? if (eregi("\.pdf",$file) || eregi("\.txt",$file) || eregi("\.jpg",$file)){ $dirFiles[] = $file; } This is one of the most ridiculous file-type checks I've ever seen. All you are doing is checking that one of those strings exists anywhere in the filename. So, ANicePicture.jpgJustKiddingImAVirus.exe is perfectly valid and passes your check. Aside from the fact that your regex doesn't work, checking the filename doesn't do any good, because the filename is not indicative of the filetype. You need to be checking the MIME type, with something like fileinfo. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 5, 2012 Share Posted September 5, 2012 also the face that eregi is a deprecated function as of php 5.3.0 Scootsah is right you should use mime types to match file extensions. mime content type Quote Link to comment Share on other sites More sharing options...
SarahB12 Posted September 5, 2012 Author Share Posted September 5, 2012 I have updated the code to what you have provided below darkfreaks, but I am receiving the same error. function filetime_callback($a, $b) { if (filemtime($a) === filemtime($b)) return 0;// always use identical comparison return filemtime($a) < filemtime($b) ? -1 : 1; } // Then sort with usort() usort($files, "filetime_callback"); Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 5, 2012 Share Posted September 5, 2012 using fileinfo with mime type extensions Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 5, 2012 Share Posted September 5, 2012 I did a quick Google search for "filemtime stat failed". The first result was to a post on this forum for the same issue and the second result was to the PHP manual for that function which also has a reference to the error. Both results would give you the reason for the error. Per the manual for filemtime() Parameters filename Path to the file. Note the text I colored in red. Your function to get the file list readdir() returns the following: Returns the name of the next entry in the directory. The entries are returned in the order in which they are stored by the filesystem. You need the complete path and file name for filemtime(). I woud suggest using glob() to get the files because 1) It returns the complete path AND 2) you can set it to only get the .pdf files by default and not need to check the extensions AND 3) It makes the code much more compact. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 5, 2012 Share Posted September 5, 2012 $dirPath = './live'; function mtime_sort($a, $b) { if (filemtime($a) == filemtime($b)) { return 0; } return (filemtime($a) > filemtime($b)) ? -1 : 1; } //Get PDF files from directory and sort $dirFiles = glob($dirPath.'/*.pdf'); usort($dirFiles, 'mtime_sort'); foreach($dirFiles as $file) { echo "<li><a href='".$file."'>".substr($file, 0, -4)."</a></li>\n"; } Quote Link to comment Share on other sites More sharing options...
SarahB12 Posted September 5, 2012 Author Share Posted September 5, 2012 Thanks for your help Psycho (and everyone else), I really appreciate you taking the time to explain this to me. ~Sarahb~ I did a quick Google search for "filemtime stat failed". The first result was to a post on this forum for the same issue and the second result was to the PHP manual for that function which also has a reference to the error. Both results would give you the reason for the error. Per the manual for filemtime() Parameters filename Path to the file. Note the text I colored in red. Your function to get the file list readdir() returns the following: Returns the name of the next entry in the directory. The entries are returned in the order in which they are stored by the filesystem. You need the complete path and file name for filemtime(). I woud suggest using glob() to get the files because 1) It returns the complete path AND 2) you can set it to only get the .pdf files by default and not need to check the extensions AND 3) It makes the code much more compact. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.