AtomicRax Posted July 3, 2010 Share Posted July 3, 2010 Currently I have: $i = 0; if ($handle = opendir($var['path'])) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $fList[$i] = $file; $i++; } } closedir($handle); } @sort($fList); for ($x = 0; $x < $i; $x++) { if(is_dir("{$var['path']}/$fList[$x]")) { $html .= "<a href=\"{$var['url']}/fileDir.php?dir=$fList[$x]\">$fList[$x]/</a><br>\n"; } else { $html .= "<a href=\"{$var['url']}/fileEdit.php?file=$fList[$x]\">$fList[$x]</a><br>\n"; } } Which opens a directory and lists all the files and folders inside. Using sort(), the listing is in alphabetical order. Everything is one alphabetized list, which looks like this: about.html cgi-bin/ events.html gallery/ I'm trying to group the directories in alphabetical order on top of the file listing, so it would sort like this: cgi-bin/ gallery/ about.html events.html Any ideas? Link to comment https://forums.phpfreaks.com/topic/206661-listing-files-with-directories-on-top/ Share on other sites More sharing options...
Pikachu2000 Posted July 3, 2010 Share Posted July 3, 2010 Just thinking out loud here. Test for the slash with strrpos, and send the results with a slash and the results without a slash into 2 separate arrays for alphabetical sorting? Link to comment https://forums.phpfreaks.com/topic/206661-listing-files-with-directories-on-top/#findComment-1080874 Share on other sites More sharing options...
AtomicRax Posted July 3, 2010 Author Share Posted July 3, 2010 For the record, I got what I wanted by using the following: $f = 0; $d = 0; // Open $var['path'] directory if ($handle = opendir($var['path'])) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && is_dir("$var[path]/$file")) { // $file is a directory $dirList[$d] = $file; $d++; } elseif ($file != "." && $file != "..") { // $file is not a directory $fileList[$f] = $file; $f++; } } closedir($handle); } @sort($fileList); @sort($dirList); for ($x = 0; $x < $d; $x++) { $html .= "<a href=\"{$var['url']}/fileDir.php?dir=$dirList[$x]\">$dirList[$x]/</a><br>\n"; } for ($x = 0; $x < $f; $x++) { $html .= "<a href=\"{$var['url']}/fileEdit.php?file=$fileList[$x]\">$fileList[$x]</a><br>\n"; } Link to comment https://forums.phpfreaks.com/topic/206661-listing-files-with-directories-on-top/#findComment-1080875 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.