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
https://forums.phpfreaks.com/topic/140003-sorting-my-directory/
Share on other sites

You'll have to read the files into an array along with their modification times (obtained using filemtime) and sort on those times. I'd probably use the name as the index of the array and the modification time as the values so sorting is simply a case of a call to rsort

Link to comment
https://forums.phpfreaks.com/topic/140003-sorting-my-directory/#findComment-732472
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
https://forums.phpfreaks.com/topic/140003-sorting-my-directory/#findComment-732474
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
https://forums.phpfreaks.com/topic/140003-sorting-my-directory/#findComment-732488
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
https://forums.phpfreaks.com/topic/140003-sorting-my-directory/#findComment-732492
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
https://forums.phpfreaks.com/topic/140003-sorting-my-directory/#findComment-732516
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.