php 获取目录中最新添加的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1491020/
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
Get the latest file addition in a directory
提问by Graviton
How to get the latest file name, or the file path that is added into a directory?
如何获取最新的文件名,或添加到目录中的文件路径?
回答by kkyy
$path = "/path/to/my/dir";
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
// now $latest_filename contains the filename of the file that changed last
回答by NDM
$dir = dirname(__FILE__).DIRECTORY_SEPARATOR;
$lastMod = 0;
$lastModFile = '';
foreach (scandir($dir) as $entry) {
if (is_file($dir.$entry) && filectime($dir.$entry) > $lastMod) {
$lastMod = filectime($dir.$entry);
$lastModFile = $entry;
}
}
回答by Ole S?rensen
filectime is for when metadata like chmod values are changed. filemtime is for actual content change.
filectime 用于更改 chmod 值等元数据。filemtime 用于实际内容更改。
回答by frank
$dir = "/path/to/Your/dir";
$pattern = '\.(zip|ZIP|pdf|PDF)$'; // check only file with these ext.
$newstamp = 0;
$newname = "";
if ($handle = opendir($dir)) {
while (false !== ($fname = readdir($handle))) {
// Eliminate current directory, parent directory
if (ereg('^\.{1,2}$',$fname)) continue;
// Eliminate other pages not in pattern
if (! ereg($pattern,$fname)) continue;
$timedat = filemtime("$dir/$fname");
if ($timedat > $newstamp) {
$newstamp = $timedat;
$newname = $fname;
}
}
}
closedir ($handle);
// $newstamp is the time for the latest file
// $newname is the name of the latest file
// print last mod.file - format date as you like
print $newname . " - " . date( "Y/m/d", $newstamp);
回答by billynoah
Here's how you can do it using DirectoryIterator:
以下是使用DirectoryIterator 的方法:
foreach(new DirectoryIterator('/path/to/read') as $item) {
if ($item->isFile() && (empty($file) || $item->getMTime() > $file->getMTime())) {
$file = clone $item;
}
}
The resulting contents of $fileis an instance of the DirectoryIterator class and as such you have access to all of it's methods. To simply get the full path of the result you can do:
的结果内容$file是 DirectoryIterator 类的一个实例,因此您可以访问它的所有方法。要简单地获得结果的完整路径,您可以执行以下操作:
echo $file->getPathname();
回答by Macci001
My solution with PHP 5:
我的 PHP 5 解决方案:
$dir = "/path/to/Your/dir";
$arraydir =scandir($dir, 1);
echo "arraydir 0: " . $arraydir[0] . "<br/>"; // 1. new file
echo "arraydir 1: " . $arraydir[1] . "<br/>"; // 2. new file
echo "arraydir elements: " . count($arraydir) . "<br/>";
(search words DE: neuste und zweitneuste Datei eines Ordners anzeigen mit PHP )
(搜索词 DE:neuste und zweitneuste Datei eines Ordners anzeigen mit PHP)
回答by Vladislav Rastrusny
Enumerate all directory files, get the filemtime() of each and you are done.
枚举所有目录文件,获取每个文件的 filemtime() 就完成了。
回答by Quamis
If working on linux, take a look at http://us2.php.net/manual/en/book.inotify.php. This assumes you leave a script waiting & logging in the background these events.
如果在 linux 上工作,请查看http://us2.php.net/manual/en/book.inotify.php。这假设您让脚本等待并在后台记录这些事件。

