SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post 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. #2
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try:
    PHP Code:
    if (is_dir($directory.$file)){ ... 
    I think is_dir is relative to the script...

    EDIT: I just threw this together to go through all subfolders until the end:
    PHP Code:
    <?php

    function writeFileList($dir) {
        
    $dir $dir."/";
        
    $folder opendir($dir);
        
        
    //ignore hidden files and ., ..
        
    echo "<ul>";
        while (
    $f readdir($folder)) {
            if (
    $f[0] != '.') {
                if (
    is_dir($dir.$f) == 1) {
                    echo 
    "<li><b>$f</b>";
                    
    writeFileList($dir.$f);
                    echo 
    "</li>";
                } else {
                    echo 
    "<li>$f</li>";
                }
            }
        }
        echo 
    "</ul>";
    }

    $base 'test-folder';
    writeFileList($base)
    ?>

  3. #3
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by funkdaddy View Post
    try:
    PHP Code:
    if (is_dir($directory.$file)){ ... 
    I think is_dir is relative to the script...

    EDIT: I just threw this together to go through all subfolders until the end:
    PHP Code:
    <?php

    function writeFileList($dir) {
    ...
    }

    $base 'test-folder';
    writeFileList($base)
    ?>
    Nice touch on the hidden folders part.

    I wrote a similar function recently. Note that this isn't true MIME type detection. Use it only for files and directories that you personally uploaded.

    PHP Code:
    $types = array('pdf' => 'Acrobat',
                   
    'chm' => 'Windows Help'
                 
    );

    function 
    fmt_filesize($num) {
        if (
    $num 1024) return $num " bytes";
        elseif ((
    $num >= 1024) && ($num 1048576)) return round($num 1024,2) . " KB";
        elseif ((
    $num >= 1048576) && ($num 1073741824)) return round($num 1048576,2) . " MB";
        else return 
    round($num 1073741824,2) . " GB";
    // fmt_filesize


    function list_dir($root_dir,$types) {
     echo 
    "<ul>\n";
     
    $dir opendir($root_dir);
     while (
    $file readdir($dir)) {
      if ((
    $file != '.') && ($file != '..')) {
       if (
    is_dir($root_dir '/' $file)) {
        echo 
    "<li><b>" $file "</b>\n";
        
    list_dir($root_dir '/' $file,$types);
        echo 
    "</li>\n";
       }
       else {
        
    $type $types[strtolower(substr($file,strrpos($file,'.')+1))];
        echo 
    "<li><a href=\"" $root_dir "/" $file "\" target=\"_blank\">" substr($file,0,strrpos($file,'.')) . "</a> &laquo; " $type " &middot; " fmt_filesize(filesize($root_dir "/" $file)) . " &raquo;</li>\n";
       }
      }
     }
     
    closedir($dir);
     echo 
    "</ul>\n";
    // list_dir

    list_dir(getcwd()); 
    I was also hoping to find a way to put the folders up top and have everything in alphabetical order without iterating over every item twice.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    function list_file $path )
    {
        
    // Normilize path
        
    $path preg_replace'~[\\\\/]+~''/'realpath$path ) . '//' );
        if ( !
    $path || file_exists$path ) ) {
            
    trigger_error'Path not found.'E_USER_WARNING );
            return 
    false;
        }
        
        
    $ret = array( 'dirs' => array(), 'files' => array() );
        
        
    $items glob$path '*' );
        foreach ( 
    $items as $item ) {
            if ( 
    is_dir$item ) ) { $ret['dirs'][] = $item; continue; }
            
    $ret['files'][] = $item;
        }
        
        return 
    $ret;
    }

    $list list_file'path/to/where/ever/' );
    foreach ( 
    $list['dirs'] as $i ) {
        
    // directories
    }

    foreach ( 
    $list['files'] as $i ) {
        
    // files
    }
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, sorry, missed the part about all the folders being at the top. I should learn to read...

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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);

    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •