2016-03-02 56 views
-1

/該png文件具有隨機名稱,所以我如何設法提取最新的文件添加?/如何訪問在php中添加到目錄的最後一個png文件?

用戶創建一個PNG文件,該文件存儲在文件夾(/ temp)中。該目錄有數百個以前創建的PNG文件。如何使用創建時間提取文件的最後添加?

+0

['filectime()'](http://php.net/manual/en/function.filectime.php),['array_multisort()'](http://php.net/manual/en/) function.array-multisort.php)。或者,將文件名和上傳時間存儲在數據庫中。 –

+2

[PHP:獲取目錄中的最新文件添加]的可能重複(http://stackoverflow.com/questions/1491020/php-get-the-latest-file-addition-in-a-directory) –

回答

0

如果你不想給你的文件命名,通過它你可以提取最後一個文件,或者,如果你不想記錄添加的最後一個文件的名稱,恐怕唯一的方法是遍歷所有文件並獲取最後修改的文件。

$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; 
    } 
} 

我從this問題得到了答案。

相關問題