Jump to content

Sorting My Directory


aMies

Recommended Posts

I am currently using a script to echo out images in a folder, using the thumbs as a display, linking to the fullsize image. How would I go about sorting them by the most recentely added? This is for a php upload filesystem.

 

		<?php

		$featured_dir = 'images/';	
		$dir = 'thumbs/';
		$scan = scandir($dir);	

		for ($i = 0; $i<count($scan); $i++) {

		if ($scan[$i] != '.' && $scan[$i] != '..') {
			if (strpos($scan[$i], '.jpg') !== false) {
				echo '
					<div id="imageList">
					<ul><a href="' . $featured_dir . $scan[$i] . '">
					<img src="' . $dir . $scan[$i] . '" alt="' . $scan[$i] . '" />
					</a>';
				echo '' . $scan[$i] .'';
				echo '</div>';
			}
		}
		}; 
		?>

Link to comment
Share on other sites

You would use the filemtime function and most likely put the results in an array using that as a key, then do a ksort on the array and then a foreach to iterate through and print the images back...

 

      <?php
         
         $featured_dir = 'images/';   
         $dir = 'thumbs/';
         $scan = scandir($dir);   
         
         for ($i = 0; $i<count($scan); $i++) {
            
           if ($scan[$i] != '.' && $scan[$i] != '..') {
            if (strpos($scan[$i], '.jpg') !== false) {
                 $fileAr[filemtime($dir . $scan[$i]]['dir'] = $dir;
                 $fileAr[filemtime($dir . $scan[$i]]['filename'] = $scan[$i];
                 $fileAr[filemtime($dir . $scan[$i]]['path'] = $dir . $scan[$i];
            }
           }
         }
         ksort($fileAr);
         foreach ($fileAr as $files) {
                 echo '
                  <div id="imageList">
                  <ul><a href="' . $featured_dir . $files['filename'] . '">
                  <img src="' . $files['path'] . '" alt="' . $files['filename'] . '" />
                  </a>';
               echo '' . $files['filename'] .'';
               echo '</div>';
         }
         ?>

 

If you want the oldest first, krsort instead.

 

Untested, but I think the logic should work.

Link to comment
Share on other sites

Yeah, I ended up writing it quite similar to what you wrote premiso, however I'm getting a Parse error on line 31

 

If you want help to fix it, I would suggest posting the code and highling or make a notation where line 31 is.

Link to comment
Share on other sites

You were just missing a ')' is all.

 

      <?php
         
         $featured_dir = 'images/';   
         $dir = 'thumbs/';
         $scan = scandir($dir);   
         
         for ($i = 0; $i<count($scan); $i++) {
            
           if ($scan[$i] != '.' && $scan[$i] != '..') {
            if (strpos($scan[$i], '.jpg') !== false) {
                 $fileAr[filemtime($dir . $scan[$i])]['dir'] = $dir;
                 $fileAr[filemtime($dir . $scan[$i])]['filename'] = $scan[$i];
                 $fileAr[filemtime($dir . $scan[$i])]['path'] = $dir . $scan[$i];
            }
           }
         }
         ksort($fileAr);
         foreach ($fileAr as $files) {
                 echo '
                  <div id="imageList">
                  <ul><a href="' . $featured_dir . $files['filename'] . '">
                  <img src="' . $files['path'] . '" alt="' . $files['filename'] . '" />
                  </a>';
               echo '' . $files['filename'] .'';
               echo '</div>';
         }
         ?>
</div>

Link to comment
Share on other sites

You were just missing a ')' is all.

 

      <?php
         
         $featured_dir = 'images/';   
         $dir = 'thumbs/';
         $scan = scandir($dir);   
         
         for ($i = 0; $i<count($scan); $i++) {
            
           if ($scan[$i] != '.' && $scan[$i] != '..') {
            if (strpos($scan[$i], '.jpg') !== false) {
                 $fileAr[filemtime($dir . $scan[$i])]['dir'] = $dir;
                 $fileAr[filemtime($dir . $scan[$i])]['filename'] = $scan[$i];
                 $fileAr[filemtime($dir . $scan[$i])]['path'] = $dir . $scan[$i];
            }
           }
         }
         ksort($fileAr);
         foreach ($fileAr as $files) {
                 echo '
                  <div id="imageList">
                  <ul><a href="' . $featured_dir . $files['filename'] . '">
                  <img src="' . $files['path'] . '" alt="' . $files['filename'] . '" />
                  </a>';
               echo '' . $files['filename'] .'';
               echo '</div>';
         }
         ?>
</div>

 

Oh, I thought you were using your code, hence the post it part =) But yea, that works.

Link to comment
Share on other sites

If you want the oldest first, krsort() instead.

 

T'other way round. The regular sorting functions sort lowest to highest; the (k)rsort functions sort highest to lowest.

 

Yea, sorry. My original line of thinking was file size lol. Thanks for correcting!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.