SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question php read dir and show files in alphabetical order

    Can anyone tell me how to show the contents of a directory in alphabetical order?
    Currently I am using the following script and it shows the files based on the date they were created.

    Code:
    <?php 
    // directory path can be either absolute or relative
    $dirPath = 'docs/';
    
    // open the specified directory and check if it's opened successfully
    if ($handle = opendir($dirPath)) {
    
       // keep reading the directory entries 'til the end
       while (false !== ($file = readdir($handle))) {
    
          // just skip the reference to current and parent directory
          if ($file != "." && $file != "..") {
             if (is_dir("$dirPath/$file")) {
                // found a directory, do something with it?
                echo "[$file]<br>";
             } else {
                // found an ordinary file
              echo "<fieldset>
      <label><a href='docs/$file'>$file</label>
    </fieldset>";
             }
          }
       }
    
       // ALWAYS remember to close what you opened
       closedir($handle);
    }
    ?>

    I've had a look around and found something about scan dir instead of read dir but I can't get it to work at all.

    As always any help is much appreciated.

    Thanks in advance

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    You could store the file and dir names in an array first, and sort that.

  3. #3
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like that would work, thanks for the super fast reply. Can you tell me how I would achieve this please? Sorry I'm not very good with arrays in php.

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Check out Salathe's SPL SortingIterator.

    PHP Code:
    <?php

    class SortingIterator extends ArrayIterator
    {
        public function 
    __construct(Traversable $iterator$callback)
        {
            if ( ! 
    is_callable($callback)) {
                throw new 
    InvalidArgumentException(sprintf('Callback must be callable (%s given).'$callback));
            }

            
    parent::__construct(iterator_to_array($iterator));
            
    $this->uasort($callback);
        }
    }

    // Sort alphabetically using PHP's strnatcasecmp function
    $it = new SortingIterator(new FilesystemIterator(__DIR__), 'strnatcasecmp');
    foreach (
    $it as $file) {
        echo 
    $file->getPathname() . PHP_EOL;
    }

    // Re-sort into decreasing file size order
    $it->uasort(function($a,$b){ return $b->getSize() - $a->getSize(); });
    foreach (
    $it as $file) {
        
    printf("%10d %s" PHP_EOL$file->getSize(), $file->getPathname());
    }

    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you tell me where in the above code do i tell it which directory to look in?

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sure, replace __DIR__
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry...I'm getting errors in the above code

    Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in the following line:

    public function __construct(Traversable $iterator, $callback)


    any ideas?

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What PHP version are you using?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Version 4.4.3

  10. #10
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ah.

    Then you're out of luck. You could try removing the public/protected/private keywords, but I think the SPL requirements would still hinder you.

    Best bet then, add the filenames to an array instead of echo'ing them out. Once you've collected all of them, sort and then display. As per Guido2004's original suggestion.

    PHP Code:
    <?php
    $array 
    = array(
      
    'orange',
      
    'apple',
      
    'grapes',
      
    'banana',
      
    'pear',
      
    'kiwi'
    );

    foreach(
    $array as $fruit){
      echo 
    $fruit "\n";
    }

    /*
      orange
      apple
      grapes
      banana
      pear
      kiwi
    */

    sort($arraySORT_STRING);

    foreach(
    $array as $fruit){
      echo 
    $fruit "\n";
    }

    /*
      apple
      banana
      grapes
      kiwi
      orange
      pear
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  11. #11
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by sketchgal View Post
    PHP Version 4.4.3
    Upgrade.
    Or tell your host to upgrade.
    Or change hosting.

  12. #12
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    For what it's worth, glob will (by default) sort the file names alphabetically and will work in your super-duper-old version of PHP.
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

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