Reading directory in php!

<?php

/*
 * Sample function to recursively return all files within a directory.
 * http://www.pgregg.com/projects/php/code/recursive_readdir.phps
 * Author: Paul Gregg
 * http://www.pgregg.com
 *
 * For a more robust and featureful recursive directory listing tool
 * have a look at preg_find:
 * http://www.pgregg.com/projects/php/preg_find/preg_find.phps
 * Example uses: http://www.pgregg.com/forums/viewtopic.php?tid=73
 */

Function listdir($start_dir=‘.’) {

  $files = array();
  if (
is_dir($start_dir
)) {
    
$fh opendir($start_dir
);
    while ((
$file readdir($fh)) !== false
) {
      
# loop through the files, skipping . and .., and recursing if necessary
      
if (strcmp($file‘.’)==|| strcmp($file‘..’)==0
) continue;
      
$filepath $start_dir ‘/’ $file
;
      if ( 
is_dir($filepath
) )
        
$files array_merge($fileslistdir($filepath
));
      else
        
array_push($files$filepath
);
    }
    
closedir($fh
);
  } else {
    
# false if the function was called with an invalid non-directory argument
    
$files false
;
  }

  return $files;

}

$files listdir(‘.’);
print_r($files
);
?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s