Jump to content

Error when sorting files in directory


SarahB12

Recommended Posts

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 ~

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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";
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.