How to get the latest file name, or the file path that is added into a directory?
A:
Enumerate all directory files, get the filemtime() of each and you are done.
FractalizeR
2009-09-29 07:13:50
I think you rather need `filectime`.
Gumbo
2009-09-29 07:17:26
+1
A:
$dir = dirname(__FILE__).DIRECTORY_SEPARATOR;
$lastMod = 0;
$lastModFile = '';
foreach (scandir($dir) as $entry) {
if (is_file($dir.$entry) && filectime($dir.$entry) > $lastmod) {
$lastMod = filectime($dir.$entry);
$lastModFile = $entry;
}
}
Nicky De Maeyer
2009-09-29 07:33:53
+2
A:
$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;
}
}
}
// now $latest_filename contains the filename of the newest file
kkyy
2009-09-29 07:34:20
A:
If working on linux, take a look at http://us2.php.net/manual/en/book.inotify.php. This assumes you leave a script waiting & logging in the background these events.
Quamis
2009-09-29 07:41:08