Jump to content

Displaying order

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Guest_Jaan_*

Guest_Jaan_*
  • Guests
hey ppl.. i have this script..:

<?php
function list_files($dir){
if(is_dir($dir)){
if($handle = opendir($dir)){
while(($file = readdir($handle)) !== false){
if($file != "." && $file != ".." && $file != "Thumbs.db"){
echo '<a target="_blank" href="'.$dir.$file.'">'.$file.'</a><br>'."\n";
}
}
closedir($handle);
}
}
}
$path = $_SERVER["APPL_PHYSICAL_PATH"];
list_files("".$path."/");
?>

and it displays my files in my account's folder.. (i mean that main folder.. public_html for example)

but it displays in alphabetic order but i want it to be.. folders first.. and then files :/

maybe someone knows how to do it.. :/

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Rather than echoing the data in the for loop, create two arrays, one to hold folder names, and the other to hold file names. After the loop, you can then print the folders array then files array...

#3
Guest_Jaan_*

Guest_Jaan_*
  • Guests
oh good idea..
but.. how.. :S

#4
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
Use the scandir function:

$files2 = scandir($dir, 1);

Which puts the DIRs first. Also, why do you not indent your code? It is hard to read because all of your code is on one line.

PHP: scandir - Manual

#5
Guest_Jaan_*

Guest_Jaan_*
  • Guests
oh thanks :D
well.. i don't know.. i have written like this since the beginning :) so.. ;D

#6
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Remember that scandir is only a part of PHP 5 and later so if you plan on using this for PHP 4 users it won't work. Here is one that works in PHP 4+5 and creates a two dimensional array for dirs and files:


<?php
    /*
        getDirTree(string $dir [, bool $showfiles]);
        $dir of the folder you want to list, be sure to have an ending /
        $showfiles set to 'false' if files shouldnt be listed in the output array
    */
    function getDirTree($dir,$p=true) {
        $d = dir($dir);$x=array();
        while (false !== ($r = $d->read())) {
            if($r!="."&&$r!=".."&&(($p==false&&is_dir($dir.$r))||$p==true)) {
                $x[$r] = (is_dir($dir.$r)?array():(is_file($dir.$r)?true:false));
            }
        }
        foreach ($x as $key => $value) {
            if (is_dir($dir.$key."/")) {
                $x[$key] = getDirTree($dir.$key."/",$p);
            }
        }
        ksort($x);
        return $x;
    }
?>

Example Output:
Array
(
    [folder1] => Array
        (
            [subfolder1] => Array()
            [subfolder2] => Array()
            [text.txt] => 1
        )

    [folder2] => Array()
    [folder3] => Array
        (
            [text.txt] => 1
        )

)

Taken from user comments at PHP: dir - Manual
</span>

#7
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
This code looks as if it could be of some use to me, nice job guys.

Indent your code :-P