PHPBuilder - Gets all Files starting at a Directory

RSS Twitter

Gets all Files starting at a Directory

by: Bernhard Bauder
August 29, 2002

Version: 1.1

Type: Function

Category: File Management

License: GNU General Public License

Description: Gets all Files starting at a Directory. this function will give you an Array back with all Fileinformation. More Information at

$file_path = "randompics/"; //dir
$with_subdirs = true; // with subdirs?
$what_file = "/.+\.(gif$|jpg$|jpeg$)/i"; // what file? (reg ex)

$pics = addfiles($file_path,$what_file,$with_subdirs);

echo implode("<br>",$pics);

function addfiles($path,$what_file,$with_subdirs)
global $files;
$files[0] = "";
$handle = @opendir($path);
while ($file = @readdir ($handle)) {
    if ($file != "." && $file != ".."){
        if ($with_subdirs) addfiles($path.$file."/",$what_file,$with_subdirs); // subdirs
        if (preg_match($what_file , $file)) $files[] = $path.$file; // addfile
@rewinddir ($handle);
unset ($files[0]);
return $files;

Comment and Contribute

Your comment has been submitted and is pending approval.

Bernhard Bauder



(Maximum characters: 1200). You have characters left.