retrieving the most recent file uploaded to a directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

retrieving the most recent file uploaded to a directory

Post by tenacious-dee »

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi

I'm using the code underneath to retrieve a list of files in a directory and display them as links on a webpage.

However, I just want to print the most recent file uploaded to that dir (ie there will be one uploaded every week and that is the only one i want to show)

Any ideas would be welcome!

thanks
Dee

Code: Select all

<?php
 
function DirDisply($data) { 
     
     $TrackDir=opendir("C:\\xampp\\htdocs\\anml\\EN\\Publications\\AnSceil\\"); 
     
     while ($file = readdir($TrackDir)) { 
     
     if (strpos($file, '.pdf',1)||strpos($file, '.PDF',1)) {   
     if ($file == "." || $file == "..") { } 
         else {
             print "<tr><td><font face=\"Verdana, Arial, Helvetica, sans-serif\"><a href=$file target=_blank>$file</a></font></td>";
             print "</td></tr><br>";
                 
                }
            }
           } 
        closedir($TrackDir); 
 
    return $data; 
    } 
                
    ?> 
       
          <b><font face="Verdana, Arial, Helvetica, sans-serif">An Scéil:</font></b>
                
         <p>
    <?  @ DirDisply($data);     ?>  
        </p>

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: retrieving the most recent file uploaded to a directory

Post by deejay »

you could maybe create a solution with filemtime[url]http://uk2.php.net/filemtime[url]
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

Re: retrieving the most recent file uploaded to a directory

Post by tenacious-dee »

Yeah I was looking at that too...

I'm fairly new to php and I'm just confused as how to structure it or fit it in with the script i'm using.

Any advice?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: retrieving the most recent file uploaded to a directory

Post by pickle »

Loop through all the files, creating an array indexed by filename, with the values being the results of filemtime(). rsort() the array & take the first one.

Also, you should run the path + filename through realpath() in each iteration of the loop - that'll eliminate the need to check manually against '.', or '..'
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: retrieving the most recent file uploaded to a directory

Post by deejay »

erm, I've never done anything like that before but ;I suppose the theory would be something like.

>> get all the files in your directory into an array that also stores the date related to the file.
file_array[name][date]
>> making sure you put the date in the format you want
date ("Y-m-d", filemtime($filename));

>> then find a way of searching through the array and find which one is closest to the the $date_now


it will take a little thought obviously but hope this helps.
Post Reply