Results 1 to 6 of 6

Thread: filesystem help

  1. #1

    Thread Starter
    Fanatic Member ubunreal69's Avatar
    Join Date
    Apr 2001
    Location
    Morayfield, Australia
    Posts
    609

    Question filesystem help

    is there a way to get the names of all the folders in a directory ?

    any help is appreciated on this topic

  2. #2
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    I don't know if there is something built into PHP, but this is what I use:

    PHP Code:
    <?php
    //Returns the contents of a dir into an array
    function dir_contents($directory$files true$directories true) {
      
    //Read all the files from the directory
      
    if ($dir opendir($directory)) {
        while (
    false !== ($file readdir($dir))) {
          
    //If we want this file, add it to the array
          
    if ((is_file($file) && $files) || (is_dir($file) && $directories))
            
    $return_value[] = $file;
        }
        
    closedir($dir);
      }
      
    //Return the files/dirs
      
    return $return_value;
    }
    ?>
    You can include/exclude files or directories and it returns them into an array.

  3. #3
    Hyperactive Member Kagey's Avatar
    Join Date
    Sep 2000
    Location
    The Wilderness of New Brunswick
    Posts
    294
    here is also another tidbit:
    PHP Code:
    if( !($dir opendir('/images')) ) die('Cannot open folder');

    while(
    $file readdir($dir)) {
        if(
    $file != '.' && $file != '..' && is_dir($file) ) {
            echo 
    $file '<br>';
        }
    }
    // note: they will not be in alphabetical order. you can add them
    // to an array then sort that array instead of printing out the
    // folder names 
    hope this helps

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I've used the code Kagey posted, but this is how I modified it:

    PHP Code:
    if ($handle opendir($dir)) {
        while (
    false !== ($file readdir($handle))) {
            if (
    $file != "." && $file != "..") {
                if (
    is_dir($file)) {
                    
    $dirs[] = $file;
                } else {
                    
    $files[] = $file;
                }
            }
        }
        
        
    closedir($handle);
        if (
    count($dirs) != 0) {
            
    sort($dirs);
        }
        if (
    count($files) != 0) {
            
    sort($files);
        }

    Now you have all the directories and all the files in two different arrays in alphabetical order.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    Hyperactive Member Kagey's Avatar
    Join Date
    Sep 2000
    Location
    The Wilderness of New Brunswick
    Posts
    294
    sweet deal

  6. #6

    Thread Starter
    Fanatic Member ubunreal69's Avatar
    Join Date
    Apr 2001
    Location
    Morayfield, Australia
    Posts
    609
    thanx guys, i'll give them all a try

Posting Permissions

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



Click Here to Expand Forum to Full Width