BigToach.com

PHP Snippets

dir_recurse

Information

(PHP Version >= 3)
Functions used in this snippet: opendir, readdir, is_dir, dir_recurse, filesize, closedir, isset.

Description

array dir_recurse( str dir, bool filesize )

Recursively find all files and folders underneath the folder dir.
If the optional filesize is set to true the array will have the indexes be the filenames, and the values be the filsize of the filenames.

Snippet

<?php
function dir_recurse($dir$filesize=false){
    if (
$fh opendir($dir)) {
        while(
false !== ($file readdir($fh))){
            if(
$file != "." && $file != "..") {
                if(
is_dir($dir '/' $file)){
                    
$data[$file] = dir_recurse($dir '/' $file$filesize);
                }else{
                    if(
$filesize){
                        
$data[$file] = filesize($dir '/' $file);
                    }else{
                        
$data[] = $file;
                    }
                }
            }
        }
        
closedir($fh);
    }
    return (isset(
$data) ? $data : array());
}
?>

Example

dir_recurse Can be used in the following way:

<?php
print_r
(dir_recurse('.')); 
// returns the directory structure of the directory that the script is in

print_r(dir_recurse('some/path/to/dir'));
// returns dir structure of some/path/to/dir

print_r(dir_recurse('.'true)); 
// returns filesizes of all files above the current working directory.

// if you have PHP 5 and you want to easily show the dir folder size you can do the following

function dummy_func($value){
    global 
$_dummy_var;
    
$_dummy_var += $value;
}
array_walk_recursive(dir_recurse('.'), 'dummy_func');
echo 
'That directory is ' $_dummy_var ' Bytes big';
?>

Added on Jul 10th at 9 pm by Scott - 5 Comments


5 Responses to dir_recurse

tovarisch_ru commented on Aug 10th at 10 am
I find error in example's code. Function dir_recurse() without second argument don't count filesize.

For all: array_walk_recursive available only since PHP 5. Use alternative function from comments on this page: http://www.php.net/manual/en/function.array-walk-recursive.php
Scott Roach commented on Aug 10th at 11 am
Works fine for me with and without the filesize flag.

Maybe there is a problem with the way that PHP 5 handles this code?
tovarisch_ru commented on Aug 10th at 12 pm
I find error in example's code. Function dir_recurse() without second argument don't count filesize.

For all: array_walk_recursive available only since PHP 5. Use alternative function from comments on this page: http://www.php.net/manual/en/function.array-walk-recursive.php
tovarisch_ru commented on Aug 10th at 12 pm
in function header $filesize=false, default

in code filesize counted only if $filesize return true

bag! :-)
tovarisch_ru commented on Aug 10th at 12 pm
in function header $filesize=false, default

in code filesize counted only if $filesize return true

bag! :-)

Name:

Site or Mail:

Comment:

To try to reduce the ammount of spam, please enter the word GOOSE in the following form

Security Code: