Web Analytics Made Easy -
StatCounter Array diplaying Folder on top ? - CodingForum

Announcement

Collapse
No announcement yet.

Array diplaying Folder on top ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Array diplaying Folder on top ?

    Good day,
    here I'm working on an array which read a directory and list the files and folders recursively.

    I'm trying to display in bold , and on top of all files, all the folders.


    PHP Code:

    <?php

    $directory 
    "Art/";
    function 
    dirList ($directory
    {

        
    // create an array to hold directory list
        
    $results = array();

        
    // create a handler for the directory
        
    $handler opendir($directory);

        
    // keep going until all files in directory have been read
        
    while ($file readdir($handler)) {

            
    // if $file isn't this directory or its parent, 
            // add it to the results array
            
    if ($file != '.' && $file != '..')
            
            
    // If file is directory, mark it in bold.
            
    if (is_dir($file)){
            echo 
    "<b>";
            echo 
    $file;
            echo  
    "</b><br/>";
            
            
    // Else not styled
            
    }else{
            echo 
    $file;
            echo  
    "<br/>";
            
        }
        }

    // Here probably should be my code for sorting the folder on top of the files.


        // tidy up: close the handler
        
    closedir($handler);

        
    // done!
        
    return $results;

    }

    dirList($directory);





    ?>
    Thanks !

  • #2
    if you are using PHP 5 then you should use scandir
    PHP Code:
    $scandir scandir('.');
    $dirs = array();
    $files = array();
    foreach (
    $scandir as $value)
    {
        if (
    $value != '.' && $value != '..')
        {
            if (
    is_dir($value)) {$dirs[$value] = $value;} else {$files[] = $value;}
        }
    }

    asort($dirs);
    asort($files);
    $all array_merge($dirs$files);

    foreach (
    $all as $value)
    {
        if (isset(
    $dirs[$value])) {$value '<b>'$value '</b>';}
        echo 
    $value '<br>';

    Comment


    • #3
      Nice code,
      At least this time I get a different result, but still, the folder are not on top of the list of files. also not in bold but that isn't really what is hard here.

      I'm stating to think that my php/server setting are wrong or something...

      Comment


      • #4
        RESOLVED

        PHP Code:

        <?php

        $directory 
        "Art/SKYDOME/";
        function 
        dirList ($directory)
        {

            
        //create 2 arrays - one for folders and one for files
           
        $folders = array();
           
        $files = array();

            
        // create a handler for the directory
            
        $handler opendir($directory);

            
        // keep going until all files in directory have been read
            
        while ($file readdir($handler)) {

                
        // if $file isn't this directory or its parent,
                // add it to the results array
                
        if ($file != '.' && $file != '..')
                
                
        // If file is directory, mark it in bold.
               
        if(is_dir($directory.$file)) { 
                
        array_push($folders,$file);
                
                
        // Else not styled
                
        }else{
                
        array_push($files,$file);
                
            }
            }


            
        // tidy up: close the handler
            
        closedir($handler);

            foreach(
        $folders as $folder) {
              echo 
        "<strong>".$folder."</strong><br />";
            }

            foreach(
        $files as $file) {
              echo 
        $file."<br />";
            }
            

        }

        dirList($directory);

        ?>

        Comment

        Working...
        X