note 44473 added to function.opendir

From: Date: Sat, 07 Aug 2004 03:17:04 +0000
Subject: note 44473 added to function.opendir
Groups: php.notes 
Request: Send a blank email to php-notes+get-74118@lists.php.net to get a copy of this message
I whipped up a little recursive directory searching function that has been very useful. I figured it belonged here. I'm sure its not the most robust or creative code, but it works for me. Note, you can use strpos for simple matches or preg_match if you want the power of regular expressions (however it is a bit slower). I also have it set to use double quotes "\\" for WIN32. change to "/" if necessary. It returns an array of all matching files with full path. function search_dir($mask,$dir,$level){ if($level > 3){ return "Exceeded max level (3)"; } $return_me = array(); if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { #echo "filename: $file : filetype: " . filetype($dir . $file) . " level: $level\n"; if(is_dir($dir."\\".$file) and $file != '.' and $file != '..'){ $test_return = search_dir($mask,$dir."\\".$file,$level+1); if(is_array($test_return)){ $temp = array_merge($test_return,$return_me); $return_me = $temp; } if(is_string($test_return)){ array_push($return_me,$test_return); } #} else if(strpos($file,$mask) !== FALSE){ #A bit faster, but you can't use regexs! } else if(preg_match($mask,$file)){ array_push($return_me,$dir."\\".$file); } } closedir($dh); } } return $return_me; } ---- Manual Page -- http://www.php.net/manual/en/function.opendir.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+44473 Delete -- http://master.php.net/manage/user-notes.php?action=delete+44473&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+44473&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#74118) next »