Welcome to the Geeks & God Static Archive. Read more »

Php Getting File Meta Information?

Joined: 11/28/2008

Hey Guys (gals),

I would like to create a PHP function that reads x amount of files from a directory, but I would like those files sorted by the date they were added to the directory. I know that the directory keeps this type of meta information, but I'm not sure how to access it using PHP. I've done a couple quick google searches but haven't found anything.

Can anyone help me out?

Joined: 11/28/2008
I recall wanting to do this

I recall wanting to do this and didn't find a very good solution, but I was able to end up with something close. I use this method for my wife's website on a page where we share some of the latest pictures at home, with the family, etc. What I did was scan the folder in question and then get the filemtime() of the file and sort the array based on that.

Who can bring a charge to God's elect? It is God who justifies!

Joined: 11/28/2008
I would recommend doing the

I would recommend doing the same thing that Mathachew had suggested. I wrote out some code to reflect what we are talking about. (I used this in a folder of images for testing.)

I hope this helps...

CODE
$files = array();
   $dir = opendir("./");
   while ($file=readdir($dir)) {
       if ($file!="." && $file!="..") {
           if (!is_dir("./".$file)) {
               array_push($files,filemtime($file)."_".$file);
           }
       }
   }
   sort($files);
   print_r($files);
Joined: 11/28/2008
Thanks for the responses

Thanks for the responses guys. I ended up using filectime() off of your suggestions. My final code ended up looking like:

CODE
foreach (glob("images/*jpg") as $path){
    $images[filectime($path)] = $path;
}
krsort($images);

Joined: 11/28/2008
If memory serves me

If memory serves me correctly, filectime() may give skewed results because it's based on when the file was changed, not necessarily when it was created or added to the server. A way to ensure the order your want is done is to modify the files from oldest to newest, but it has the possibility of being off, which is why I ended up going with filemtime(). But hey, if it works, good /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

Who can bring a charge to God's elect? It is God who justifies!

Joined: 11/28/2008
Ah, good call. Thanks for the

Ah, good call. Thanks for the heads up.