A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: PHP - Sort files by Date?

  1. #1
    Juvenile Delinquent CVO Chris's Avatar
    Join Date
    Jul 2002
    Location
    Ulster, UK
    Posts
    520

    PHP - Sort files by Date?

    I have a site that contains a folder with which a client will upload images to. The web site is to display only the latest 3 images that were uploaded to the folder. I have the script below but it is showing the first 3 images in the folder in alphabetical order.

    Is php able to access the date modified property or date created property of a file and sort that way?

    I've never touched PHP but it has been forced upon me in the cruellest fashion. Any help would be appreciated

    PHP Code:
    <?php
        $images 
    "advertisingExamples/thumbs/"# Location of small versions
        
    $big    "../../advertisingExamples/big/"# Location of big versions (assumed to be a subdir of above)
        
        
    if ($handle opendir($images)) {
           while (
    false !== ($file readdir($handle))) {
               if (
    $file != "." && $file != ".." && $file != rtrim($big,"/")) {
                   
    $files[] = $file;
               }
           }
           
    closedir($handle);
        }
        
    --- 
    BELOW IS THE LOOP THAT OUTPUTS THE IMAGES BUT IT IS IN ALPHABETICAL ORDER ---
        
    $i 0;
        foreach(
    $files as $file)
        {
          echo 
    '<td align="center"><a href="' $images $big $file '"><img src="' $images $file '" /></a></td>';
          if (++
    $i == 3) break;
        }
    ?>
    Last edited by CVO Chris; 08-17-2010 at 07:06 PM. Reason: Terrible grammar!

  2. #2
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    change
    Code:
    $files[] = $file;
    into
    Code:
    $files[] = filemtime("$images/$file");
    and then later on
    Code:
    --- BELOW IS THE LOOP THAT OUTPUTS THE IMAGES
    arsort($files);
    $i = 0;
    foreach($files as $file => $mtime)
    Musicman

  3. #3
    Juvenile Delinquent CVO Chris's Avatar
    Join Date
    Jul 2002
    Location
    Ulster, UK
    Posts
    520
    Thank musicman but I think something is wrong as it is now not outputting any images.
    PHP Code:
        <?php
        $images 
    "advertisingExamples/thumbs/"# Location of small versions
        
    $big    "../../advertisingExamples/big/"# Location of big versions (assumed to be a subdir of above)
        
        
    if ($handle opendir($images)) {
           while (
    false !== ($file readdir($handle))) {
               if (
    $file != "." && $file != ".." && $file != rtrim($big,"/")) {
                   
    $files[] = filemtime("$images/$file");
               }
           }
           
    closedir($handle);
        }
        
        
    arsort($files);
        
    $i 0;
        foreach(
    $files as $file => $mtime)
        {
          echo 
    '<td align="center"><a href="' $images $big $file '"><img src="' $images $file '" /></a></td>';
          if (++
    $i == 3) break;
        }
    ?>
    Edit - It's outputting the following on the web page.
    Code:
    <td align="center">
    <a href="advertisingExamples/thumbs/../../advertisingExamples/big/5">
    <img src="advertisingExamples/thumbs/5" /></a></td><td align="center">
    <a href="advertisingExamples/thumbs/../../advertisingExamples/big/4">
    <img src="advertisingExamples/thumbs/4" /></a></td><td align="center">
    <a href="advertisingExamples/thumbs/../../advertisingExamples/big/3">
    <img src="advertisingExamples/thumbs/3" /></a>
    </td>
    For some reason the filenames are numbers (5, 4 and 3). Also the folders are wrong. It should output:
    Code:
    <td align="center">
    <a href="advertisingExamples/advertisingExamples/big/newest.jpg">
    <img src="advertisingExamples/thumbs/newest.jpg" /></a></td><td align="center">
    <a href="advertisingExamples/advertisingExamples/big/2ndnew.jpg">
    <img src="advertisingExamples/thumbs/2ndnew.jpg" /></a></td><td align="center">
    <a href="advertisingExamples/advertisingExamples/big/oldest.jpg">
    <img src="advertisingExamples/thumbs/oldest.jpg" /></a></td>
    Last edited by CVO Chris; 08-18-2010 at 10:27 AM.

  4. #4
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    sorry, should have been
    Code:
    $files[$file] = filemtime("$images/$file");
    Musicman

  5. #5
    Juvenile Delinquent CVO Chris's Avatar
    Join Date
    Jul 2002
    Location
    Ulster, UK
    Posts
    520
    Works perfectly. Thanks for your time and expertise

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center