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

Posted on 2012-08-20
Last Modified: 2012-08-20

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
Question by:piixeldesigns
    LVL 34

    Accepted Solution

    $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;
    LVL 1

    Author Closing Comment

    Thanks Works Perfectly!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now