Find the answer to your Linux question:
Results 1 to 3 of 3
I tried to use the opendir function in PHP and got the contents of the library... but the problem now is that.. the contents of the directory is not sorted.. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2006
    Posts
    15

    PHP - How to open a sorted directory files


    I tried to use the opendir function in PHP and got the contents of the library... but the problem now is that.. the contents of the directory is not sorted..

    how can I open the directory and sort the contents of it .. ? ...

    this is the code on how I opened it..

    Code:
    $DirectoryPointer = opendir($DirectoryName);
    		
    $image_count = 0;
    while($item = readdir($DirectoryPointer))
     {
     print $item
    }

  2. #2
    Just Joined! Tomato's Avatar
    Join Date
    Aug 2006
    Location
    HKSAR, China
    Posts
    63
    What version is your PHP first ?

  3. #3
    Just Joined! Tomato's Avatar
    Join Date
    Aug 2006
    Location
    HKSAR, China
    Posts
    63
    Code:
    function readdirsort($dir) {
           /* Funktastic readdir wrapper. Puts directories first and sorts. */
           static $not_first_run, $last_read_item=0, $items;
    
           if (!$not_first_run) {
                   $not_first_run = true;
                   while (false !== ($file = readdir($dir))) {
                           if (is_dir("$file"))
                                   $dirs[] = $file;
                           else
                                   $files[] = $file;
                   }
    
                   natcasesort($dirs);
                   natcasesort($files);
    
                   $items = array_merge($dirs, $files);
           }
    
           if ($items[$last_read_item] == false)
                   return false;
           else
                   return $items[$last_read_item++];
    }
    Try this code.
    For further reference, you can click this link
    http://hk.php.net/readdir

    I hope this can give you a hand

Posting Permissions

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