tags:

views:

128

answers:

4

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
I think you rather need `filectime`.
Gumbo
+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
Never mind, I see you fixed it :)
kkyy
it's morning here :)
Nicky De Maeyer
+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
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