If you don't want to give a name to your files through which you can extract the last one, or, if you don't want to record somewhere the name of the last file added, I'm afraid the only way is to traverse through all files and get the last modified one.
$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;
}
}
I got the answer from this question.