Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the latest file addition in a directory

Tags:

php

How to get the latest file name, or the file path that is added into a directory?

like image 218
Graviton Avatar asked Sep 29 '09 07:09

Graviton


1 Answers

$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 file that changed last 
like image 133
kkyy Avatar answered Oct 14 '22 00:10

kkyy