Jump to content

Recommended Posts

I have a function for displaying files and folders within a directory. I have added the filemtime() function to display the time the file was last modified but I'm receiving this warning and I am having no luck with finding out how to fix it on google:

 

Warning: filemtime() [function.filemtime]: stat failed for mail.php in /home/eliteace/public_html/updatez/includes/functions.inc.php on line 41

note: this is just 1 warning, the function returns about 40 files all with the same warning.

 

 

here's my script:

<?php
function display_folder($dirname) {

   $dir = opendir($dirname);
   
   $file_list = '';
   
   while(false !== ($file = readdir($dir))) {
      if(($file != ".") and ($file != "..")) {
         if(is_dir($dirname . '/' . $file)) {
            $file_list .= "<li style=\"color: red\">{$file}\n<ul>";
            $file_list .= display_folder($dirname . '/' . $file);
            $file_list .= "</ul>\n</li>\n";
         }
         elseif(substr($file,"-4") == ".php") {
            $file_list .= "<li style=\"color: aqua;\">$file</li>" . date("H, d M",strtotime(filemtime($file))) . "\n";
         }
         else {
            $file_list .= "<li style=\"color: yellow;\">$file</li>" . date("H, d M",strtotime(filemtime($file))) . "\n";
         }
      }
   }
   closedir($dir);
   return $file_list;
}

 

any help is greatly appreciated.

 

Regards ACE

Link to comment
https://forums.phpfreaks.com/topic/134708-warning-filemtime-stat-failed-for/
Share on other sites

Files for internet are "/" while files on a server are "\" If your trying to get all on the server try "\"

No, backslashes, "\", are used on Windows machines for file paths. PHP will accept a forward slash "/" and do the right thing on a Windows machine. There is no need to use backslashes when programming PHP.

 

Ken

$file is the actual file.

$dirname is the directory.  They are relative to the script being run, so they need to be combined to work.

 

 

For example some/folder/then/file.txt.  $file would be file.txt and $dirname would be some/folder/then.

 

 

So, you need date("H, d M",strtotime(filemtime($dirname . '/' . $file)))

you need date("H, d M",strtotime(filemtime($dirname . '/' . $file)))

 

That has got rid of the Warnings. But it is showing every date as...

18, 31 Dec

 

Which isn't right, the files have been modified at different times, and in the past few months. Not the future in December 31  ???

  • 3 years later...

I know this is a late reply, but often there's a pesky newline character in your variable sent to filemtime() or other functions.

 

I see this a lot when globbing. The code below removes those line endings. Both \r and \n.

 

$files = glob("*.jpg");
foreach ($files as $file) {
   $file = preg_replace("/(\r|\n)/", "", $file);
   $mtime = date("Y-m-d H:i:s", filemtime($file));
   print($mtime . "\n");
}

Guest
This topic is now closed to further replies.
×
×
  • 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.