0

Possible Duplicate:
PHP readdir() not returning files in alphabetical order

When uploaded to the hosting server, Readdir does not seem to read files alphabetically. I kinda named my files in a particular order so I really need the feature.

Here is my code:

if (is_dir($dir)) {

            if ($dh = opendir($dir)) {  

                while (($file = readdir($dh)) !== false) {

                    $ext = pathinfo($dir.$file,PATHINFO_EXTENSION);

                    if ($ext == 'jpg' || $ext == 'png') {

                        $dataArr = split('\.',$file);

                        $file = $dataArr[0];



                        ?>
                        <li>
                          <a href="/test/img/picture gallery/<?php echo $folder.'/'; ?><?php echo $file; ?>.jpg">
                            <img src="/test/img/picture gallery/<?php echo $folder.'/thumbs/t'; ?><?php echo $fileFirst; ?>.jpg" title="" class="image0">
                          </a>
                        </li>
                        <?php
                    }

                }  

                closedir($dh);  

            }  

        }

Any ideas on how to make this possible? Thanks!

EDIT:

Fixed.

I tried saving them into an array and using the sort() function of PHP to well, sort it alphabetically. It appears that the file system does not read files alphabetically.

Community
  • 1
  • 1
AJ Naidas
  • 1,424
  • 7
  • 25
  • 47
  • 2
    See http://stackoverflow.com/questions/541510/php-readdir-not-returning-files-in-alphabetical-order – Mike Aug 03 '12 at 08:48
  • That's expected behaviour. I think it is even mentioned in the PHP docs of readdir. – knittl Aug 03 '12 at 09:22
  • [`glob()`](http://php.net/manual/en/function.glob.php) returns items sorted in alphabetical order by default. My answer [here](http://stackoverflow.com/questions/10207191/how-to-sort-files-by-modified-date-through-php/10207413#10207413) provides a function that allows you to sort by other attributes (size and created/modified/accessed time) as well as the file name. – DaveRandom Aug 03 '12 at 10:56

0 Answers0