php 使用PHP获取目录中文件的最后修改日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11853935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting last modification date of files in directory using PHP
提问by Nick
I am trying to get the last modification date of all files in a directory using PHP.
我正在尝试使用 PHP 获取目录中所有文件的最后修改日期。
I am using this:
我正在使用这个:
foreach($dir as $file)
{
$mod_date=date("F d Y H:i:s.", filemtime($file));
}
foreach($dir as $file)is returning the correct files, but all of the modification dates are coming back as 0000-00-00 00:00:00, instead of the actual modification date.
foreach($dir as $file)正在返回正确的文件,但所有修改日期都返回为 0000-00-00 00:00:00,而不是实际修改日期。
What changes do I need to make to get this working?
我需要进行哪些更改才能使其正常工作?
回答by raidenace
Check if the $file var is actually pointing to a correct file
检查 $file var 是否实际指向正确的文件
foreach($dir as $file)
{
if(is_file($file))
{
$mod_date=date("F d Y H:i:s.", filemtime($file));
echo "<br>$file last modified on ". $mod_date;
}
else
{
echo "<br>$file is not a correct file";
}
}
回答by Prasanth
date("F d Y H:i:s.", false)is what you are getting. see documentationof filemtime. It returns false on failure.
date("F d Y H:i:s.", false)是你得到的。看到文件的filemtime。它在失败时返回 false。
回答by Monseur Seye
Rather than use globfunction, why not use scandirfunction.
Secondly, you could easily get the date format you want by using date("Y-m-d H:i:s", filemtime($file))
与其用glob函数,不如用scandir函数。其次,您可以通过使用轻松获得所需的日期格式date("Y-m-d H:i:s", filemtime($file))

