php擷取一個檔案夾的mtime的程式

來源:互聯網
上載者:User

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);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.