Jump to content

Listing Files, with directories on top?


AtomicRax

Recommended Posts

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

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";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.