Last active
March 30, 2016 10:10
-
-
Save corazzi/33d6ef28f2d1a97f7c12 to your computer and use it in GitHub Desktop.
Get the directory's modified time based on when the last file or directory within it was updated
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get the directory's modified time based on when the last file within it was updated | |
* | |
* @param string Which directory to look within | |
* @param string A regular expression to match files | |
* | |
* @return int The timestamp | |
*/ | |
function dirmtime($path = '', $regex = null) | |
{ | |
$directory = new RecursiveDirectoryIterator($path); | |
$iterator = new RecursiveIteratorIterator($directory); | |
if ($regex) { | |
$iterator = new RegexIterator($iterator, $regex, RecursiveRegexIterator::GET_MATCH); | |
} | |
$mtimes = []; | |
foreach ($iterator as $file) { | |
if (is_array($file)) { | |
foreach ($file as $f) { | |
$mtimes[] = filemtime($f); | |
} | |
continue; | |
} | |
$mtimes[] = filemtime($file); | |
} | |
return empty($mtimes) ? null : max($mtimes); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment