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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.