MasterACE14 Posted November 29, 2008 Share Posted November 29, 2008 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 More sharing options...
vicodin Posted November 29, 2008 Share Posted November 29, 2008 i could be wrong but it looks like you are trying to run that function on a directory instead of a file. Thats what i am seeing but i could be wrong. Link to comment https://forums.phpfreaks.com/topic/134708-warning-filemtime-stat-failed-for/#findComment-701434 Share on other sites More sharing options...
ShiloVir Posted November 29, 2008 Share Posted November 29, 2008 Files for internet are "/" while files on a server are "\" If your trying to get all on the server try "\" Link to comment https://forums.phpfreaks.com/topic/134708-warning-filemtime-stat-failed-for/#findComment-701447 Share on other sites More sharing options...
kenrbnsn Posted November 29, 2008 Share Posted November 29, 2008 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 Link to comment https://forums.phpfreaks.com/topic/134708-warning-filemtime-stat-failed-for/#findComment-701500 Share on other sites More sharing options...
corbin Posted November 29, 2008 Share Posted November 29, 2008 $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))) Link to comment https://forums.phpfreaks.com/topic/134708-warning-filemtime-stat-failed-for/#findComment-701534 Share on other sites More sharing options...
MasterACE14 Posted November 30, 2008 Author Share Posted November 30, 2008 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 ??? Link to comment https://forums.phpfreaks.com/topic/134708-warning-filemtime-stat-failed-for/#findComment-702168 Share on other sites More sharing options...
kenrbnsn Posted November 30, 2008 Share Posted November 30, 2008 The filemtime function returns a UNIX timestamp, so you don't have to use the strtotime function. <?php echo date("H, d M",filemtime($dirname . '/' . $file)); ?> Ken Link to comment https://forums.phpfreaks.com/topic/134708-warning-filemtime-stat-failed-for/#findComment-702246 Share on other sites More sharing options...
wsams Posted October 15, 2012 Share Posted October 15, 2012 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"); } Link to comment https://forums.phpfreaks.com/topic/134708-warning-filemtime-stat-failed-for/#findComment-1385381 Share on other sites More sharing options...
wsams Posted October 15, 2012 Share Posted October 15, 2012 (edited) Just want to note that the same thing happens for file() calls, but you can supply a second argument like the following to prevent that, <?php $files = file("my.log", FILE_IGNORE_NEW_LINES); Edited October 15, 2012 by wsams Link to comment https://forums.phpfreaks.com/topic/134708-warning-filemtime-stat-failed-for/#findComment-1385387 Share on other sites More sharing options...
Christian F. Posted October 15, 2012 Share Posted October 15, 2012 "Late reply"..? It's 4 years! Link to comment https://forums.phpfreaks.com/topic/134708-warning-filemtime-stat-failed-for/#findComment-1385391 Share on other sites More sharing options...
Pikachu2000 Posted October 15, 2012 Share Posted October 15, 2012 Notwithstanding the fact that this thread is OLD, trim will also remove linefeeds/newlines, along with spaces and others, without the expense of a regex pattern. Link to comment https://forums.phpfreaks.com/topic/134708-warning-filemtime-stat-failed-for/#findComment-1385396 Share on other sites More sharing options...
Recommended Posts