Pyh.conf’25: a new PHP conference for the Russian-speaking community

Voting

: eight minus four?
(Example: nine)

The Note You're Voting On

info at daniel-marschall dot de
16 years ago
It could be useful to determinate the timestamp of the newest file in a directory. (e.g. if you want to find out when the last change was made to your project).

Following function will help you:

<?php

function getAllFiles($directory, $recursive = true) {
$result = array();
$handle = opendir($directory);
while (
$datei = readdir($handle))
{
if ((
$datei != '.') && ($datei != '..'))
{
$file = $directory.$datei;
if (
is_dir($file)) {
if (
$recursive) {
$result = array_merge($result, getAllFiles($file.'/'));
}
} else {
$result[] = $file;
}
}
}
closedir($handle);
return
$result;
}

function
getHighestFileTimestamp($directory, $recursive = true) {
$allFiles = getAllFiles($directory, $recursive);
$highestKnown = 0;
foreach (
$allFiles as $val) {
$currentValue = filemtime($val);
if (
$currentValue > $highestKnown) $highestKnown = $currentValue;
}
return
$highestKnown;
}

// Use example

echo 'The newest file has the time stamp:<br>';
echo
date('Y-m-d H:i:s', getHighestFileTimestamp('../'));

?>

<< Back to user notes page

To Top