note 35196 added to ref.filesystem

From: Date: Fri, 22 Aug 2003 22:23:00 +0000
Subject: note 35196 added to ref.filesystem
Groups: php.notes 
Request: Send a blank email to php-notes+get-54890@lists.php.net to get a copy of this message
This function searches a directory and returns an array of all files whose filename matches the specified regular expression. It's similar in concept to the Unix find program. function findfile($location='',$fileregex='') { if (!$location or !is_dir($location) or !$fileregex) { return false; } $matchedfiles = array(); $all = opendir($location); while ($file = readdir($all)) { if (is_dir($location.'/'.$file) and $file <> ".." and $file <> ".") { $subdir_matches = findfile($location.'/'.$file,$fileregex); $matchedfiles = array_merge($matchedfiles,$subdir_matches); unset($file); } elseif (!is_dir($location.'/'.$file)) { if (preg_match($fileregex,$file)) { array_push($matchedfiles,$location.'/'.$file); } } } closedir($all); unset($all); return $matchedfiles; } $htmlfiles = findfile('/some/dir','/\.(htm|html)$/'); ---- Manual Page -- http://www.php.net/manual/en/ref.filesystem.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35196 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35196&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35196&report=yes

« previous php.notes (#54890) next »