如何獲取最新的文件名,或者是添加到目錄中的文件路徑的最新文件除?獲得目錄
獲得目錄
回答
$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
列舉所有目錄中的文件,讓每個filemtime(),和你做。
我想你最好'filectime'。 – Gumbo 2009-09-29 07:17:26
@Gumbo:爲什麼使用filectime而不是filemtime。我發現filemtime()在多種平臺上工作得更好,而filectime()似乎在所有Windows服務器上都不能正常工作。 – secretlm 2013-06-19 10:17:03
@secretlm'filectime'可能不起作用,因爲它只有文件的創建時間,而不是修改時間 – FluorescentGreen5 2017-06-12 07:16:56
$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;
}
}
如果在Linux上工作,看看http://us2.php.net/manual/en/book.inotify.php。這假設你讓腳本等待&在後臺登錄這些事件。
$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);
這將使用折舊功能:額日格() – 2014-08-01 15:42:29
filectime用於像chmod值這樣的元數據發生更改時。 filemtime用於實際內容更改。
我與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)
- 1. os.walk獲得目錄名
- 2. 獲得目錄名和UNIX
- 3. 獲得兩個目錄
- 4. PHP獲得創建目錄
- 5. 如何獲得目錄c#
- 6. 如何獲得/ etc /目錄
- 7. 獲得一個目錄的父目錄中的Bash
- 8. 如何獲得iPhone應用文檔目錄的目錄樹
- 9. CMake的:如何獲得子目錄從根目錄
- 10. 如何獲得目錄名在PHP
- 11. Jsoup wiki scraper如何獲得目錄框
- 12. 谷歌目錄API - 如何獲得BatchRequest
- 13. 獲得iPhone的文檔目錄
- 14. 如何獲得完整的目錄?
- 15. 關於「楠::獲得基目錄()」
- 16. C#中無法獲得子目錄
- 17. 如何獲得的父目錄位置
- 18. 蟒蛇OS:獲得某個目錄
- 19. 獲得子目錄列表中的MSBuild
- 20. 如何獲得當前工作目錄
- 21. 獲得來自WordPress插件目錄
- 22. 的iOS:無法獲得緩存目錄
- 23. 如何獲得根目錄在PHP
- 24. 如何獲得目錄plugin_dir_path在WordPress
- 25. 獲得具有在其目錄名稱
- 26. EF獲得每個項目的記錄
- 27. PHP僅獲得目錄路徑
- 28. WindowsError試圖獲得目錄與os.listdir()
- 29. 如何從SCORM 1.2中獲得目錄
- 30. 如何獲得當前項目目錄或bin目錄和移動幾級了?
[ctime並非創建時間] (http://userprimary.net/posts/2007/11/18/ctime-in-unix-means-last-change-time-not-create-time/) – kojiro 2012-01-12 21:46:55
爲最快的搜索或掃描任何新的方法? – Muhaimin 2015-01-14 03:17:49
@Muhaimin阿卜杜勒嘗試SCANDIR()〜PHP 5 – 2016-03-08 10:03:04