0

how to sort out folder directory by alphabetically using php please help me to solve this problem and this is not [duplicate] post thanks.

this code is showing like this (sort by time)

  • Dog
  • Cat
  • Orange
  • Apple
  • Doll
  • Fish
  • Airplane
  • Banana
  • Elephant

And i want like this

ABC

  • Airplane
  • Apple
  • Apple
  • Banana
  • Cat

DEF

  • Dog
  • Doll
  • Elephant
  • Fish

php code

<?php
    function folderlist(){
      $startdir = './';
      $ignoredDirectory[] = '.'; 
      $ignoredDirectory[] = '..';
       if (is_dir($startdir)){
           if ($dh = opendir($startdir)){
               while (($folder = readdir($dh)) !== false){
                   if (!(array_search($folder,$ignoredDirectory) > -1)){
                     if (filetype($startdir . $folder) == "dir"){
                           $directorylist[$startdir . $folder]['name'] = $folder;
                           $directorylist[$startdir . $folder]['path'] = $startdir;
                       }
                   }
               }
               closedir($dh);
           }
       }
    return($directorylist);
    }


    $folders = folderlist();
    sort($folders);
        foreach ($folders as $folder){
        $path = $folder['path'];
        $name = $folder['name'];




    echo '<div class="menu">
    <h3 class="headerbar"><a href="' .$path .'index2.php?folder=' .$name . '" class="style1"><span class="headertit">' .$name . '</span></a></div>';
      }
    ?>
user1796164
  • 131
  • 1
  • 4
  • 14
  • 1
    It is a little bit duplicated http://stackoverflow.com/questions/884974/sort-and-display-directory-list-alphabetically-using-opendir-in-php ;) – Antony D'Andrea Dec 04 '14 at 16:58

2 Answers2

0

Replace your call to sort with something like this:

usort($folders, function ($a, $b) {
    return strnatcmp($a['name'], $b['name']);
}); 

Additionally you might want to reduce your code complexity by using DirectoryIterator or scandir

Rangad
  • 2,110
  • 23
  • 29
  • Without knowing your error message, no one can help you. The code posted should work. Example: http://3v4l.org/gc2qg – Rangad Dec 04 '14 at 17:06
  • You deleted to much. The way your code currently stands will never call your function. The line that used to be above the call to `sort` should've remained. Add the `$folders = folderlist();` back before the call to `usort`. – Rangad Dec 04 '14 at 17:13
  • well i change again code and check on localhost it is showing this error Parse error: syntax error, unexpected T_FUNCTION in /in/qsaZX on line 24 and i also check on my website it is showing simple listing and also my website web it is showing error with simple listing Warning: sort() expects parameter 2 to be long, object given in /home/user/public_html/dir.php on line 26 – user1796164 Dec 04 '14 at 17:23
  • If you got unexpected T_FUNCTION you either messed up while copying the code or you use an (very very) outated version of php on your localhost. For the second error: Why do you call sort? If you want to use the code posted you need to use `usort`. – Rangad Dec 04 '14 at 17:35
0

You should try using scandir(). By default, scandir() will sort files alphabetically:

$dir    = './';
$files1 = scandir($dir);

print_r($files1);

http://php.net/manual/en/function.scandir.php

Anthony
  • 107
  • 5