PHP get the filename of the last file uploaded into a directory


I have a .xml file which gets uploaded with a different filename each week, and I want my script to know and use the file name of the most recent update.

Is there a way to do this through PHP? And it must be able to be flexable to change to the correct filename each time a different file is uploaded.

Thanks In Advance
Who is Participating?
gr8gonzoConnect With a Mentor ConsultantCommented:
$files = array();
$dir = "/path/to/dir/containing/the/files";
$dh = opendir($dir);
while(($file = readdir($dh)) !== false)
  if(($file == ".") || ($file == "..")) continue;
  $full = $dir . "/" . $file;

  $files[filemtime($full)] = $full;
$latestFile = end($files);

echo "Latest modified file = " . $latestFile;
piixeldesignsAuthor Commented:
Thanks Works Perfectly!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.