Как получить последнее имя файла или путь к файлу, который добавлен в каталог?
PHP: получить последнее добавление файла в каталоге
Ответ 1
$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
Ответ 2
$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;
}
}
Ответ 3
filectime - это когда метаданные, такие как значения chmod, изменяются. filemtime - для фактического изменения содержимого.
Ответ 4
$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);
Ответ 5
Перечислить все файлы каталога, получить файлmmy() каждого из них, и вы закончили.
Ответ 6
Если вы работаете в Linux, посмотрите http://us2.php.net/manual/en/book.inotify.php. Это предполагает, что вы оставите script ожидания и записи в фоновом режиме этих событий.
Ответ 7
Мое решение с 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/>";
(слова поиска DE: neuste und zweitneuste Datei eines Ordners anzeigen mit PHP)