Page 1 of 1

retrieving the most recent file uploaded to a directory

Posted: Wed Jan 28, 2009 8:41 am
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.

Re: retrieving the most recent file uploaded to a directory

Posted: Wed Jan 28, 2009 9:15 am
by deejay
you could maybe create a solution with filemtime[url]http://uk2.php.net/filemtime[url]

Re: retrieving the most recent file uploaded to a directory

Posted: Wed Jan 28, 2009 9:25 am
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?

Re: retrieving the most recent file uploaded to a directory

Posted: Wed Jan 28, 2009 10:06 am
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 '..'

Re: retrieving the most recent file uploaded to a directory

Posted: Wed Jan 28, 2009 10:12 am
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.