php很容易擷取到一個檔案夾的mtime,可以使用filemtime函數。但檔案夾的filemtime由於linux的原因。檔案修改時,只會影響他當前的檔案夾mtime變化。並不會繼續影響檔案夾上層的檔案夾。所以可能跟真正想得到的檔案夾的最後修改時間不同。
看linux機器上的效果如下, 可以看到yoyoTmp的mtime小於yoyoTmp/test的mtime。
[root@localhost test]# ls -ld --full-time /yoyoTmp/
drwxr-xr-x. 4 root root 34 2015-12-01 21:09:47.526804049 +0800 /yoyoTmp/
[root@localhost test]# ls -ld --full-time /yoyoTmp/test
drwxr-xr-x. 2 root root 20 2015-12-01 21:15:22.266131826 +0800 /yoyoTmp/test
也可使用stat 檔案夾命令查看mtime之類資訊
google得知一個phper實現了如下 原文連結:
function dirmtime($directory) {
// 1. An array to hold the files.
$last_modified_time = 0;
// 2. Getting a handler to the specified directory
$handler = opendir($directory);
// 3. Looping through every content of the directory
while ($file = readdir($handler)) {
// 3.1 Checking if $file is not a directory
if(is_file($directory.DIRECTORY_SEPARATOR.$file)){
$files[] = $directory.DIRECTORY_SEPARATOR.$file;
$filemtime = filemtime($directory.DIRECTORY_SEPARATOR.$file);
if($filemtime>$last_modified_time) {
$last_modified_time = $filemtime;
}
}
}
// 4. Closing the handle
closedir($handler);
// 5. Returning the last modified time
return $last_modified_time;
}
Example
This example demonstrates how to find the last modified time of the directory, where the working PHP script file resides, and print the result to the screen.
PHP. Print the Last Modified Time of Current Directory ?
$directory = dirname(__FILE__);
$dir_last_modified_time = dirmtime($directory);
echo date('d M Y h:i:s', $dir_last_modified_time);