How to get the latest file name, or the file path that is added into a directory?
Asked
Active
Viewed 5.0k times
35
-
filectime is for when metadata like chmod values are changed. filemtime is for actual content change. – Ole Sørensen Jul 30 '13 at 16:50
-
To clear up any confusion: The [`c` in `filectime()` does not stand for `creation`](https://www.php.net/manual/en/function.filectime.php) and the [`C` in `getCTime()` does not stand for `creation`](https://www.php.net/manual/en/splfileinfo.getctime.php) – mickmackusa May 31 '23 at 07:22
6 Answers
59
$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
-
9[ctime is not creation time](http://userprimary.net/posts/2007/11/18/ctime-in-unix-means-last-change-time-not-create-time/) – kojiro Jan 12 '12 at 21:46
-
1
-
-
2As others have mentioned in other answers... should probably be using `filemtime` instead of `filectime`. a) it's more widely supported across platforms. b) It returns what you're probably wanting: the time that the file was last modified – Brad Kent Apr 07 '16 at 14:04
3
$dir = "/path/to/Your/dir";
$pattern = '\.(zip|ZIP|pdf|PDF)$'; // check only file with these ext.
$newstamp = 0;
$newname = "";
if ($handle = opendir($dir)) {
while (false !== ($fname = readdir($handle))) {
// Eliminate current directory, parent directory
if (ereg('^\.{1,2}$',$fname)) continue;
// Eliminate other pages not in pattern
if (! ereg($pattern,$fname)) continue;
$timedat = filemtime("$dir/$fname");
if ($timedat > $newstamp) {
$newstamp = $timedat;
$newname = $fname;
}
}
}
closedir ($handle);
// $newstamp is the time for the latest file
// $newname is the name of the latest file
// print last mod.file - format date as you like
print $newname . " - " . date( "Y/m/d", $newstamp);

frank
- 41
- 3
3
Here's how you can do it using DirectoryIterator:
foreach(new DirectoryIterator('/path/to/read') as $item) {
if ($item->isFile() && (empty($file) || $item->getMTime() > $file->getMTime())) {
$file = clone $item;
}
}
The resulting contents of $file
is an instance of the DirectoryIterator class and as such you have access to all of it's methods. To simply get the full path of the result you can do:
echo $file->getPathname();

But those new buttons though..
- 21,377
- 10
- 81
- 108
1
My solution with PHP 5:
$dir = "/path/to/Your/dir";
$arraydir =scandir($dir, 1);
echo "arraydir 0: " . $arraydir[0] . "<br/>"; // 1. new file
echo "arraydir 1: " . $arraydir[1] . "<br/>"; // 2. new file
echo "arraydir elements: " . count($arraydir) . "<br/>";
(search words DE: neuste und zweitneuste Datei eines Ordners anzeigen mit PHP )

Odin Thunder
- 3,284
- 2
- 28
- 47

Macci001
- 11
- 1
-
This sorts by name, will work only if you have alphabetically sortable timestamps in the filename, like `exported_data_20170721.csv` – user Oct 10 '18 at 14:23
0
Enumerate all directory files, get the filemtime() of each and you are done.

Vladislav Rastrusny
- 29,378
- 23
- 95
- 156
-
1
-
1@Gumbo: why use filectime instead of filemtime. I see filemtime() work better on a multitude of platforms while filectime() does not seem to work properly on all Windows servers. – secretlm Jun 19 '13 at 10:17
-
1@secretlm `filectime` probably doesn't work because its only the creation time of the file, not the modification time – FluorescentGreen5 Jun 12 '17 at 07:16