note 96195 added to function.readdir
| From: | support at addcomment dot ru | Date: | Sat, 13 Feb 2010 17:34:41 +0000 |
| Subject: | note 96195 added to function.readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165937@lists.php.net to get a copy of this message | ||
optimized and correct function for recursive directory listing:
<?
$__dirs=array();
function getdirr($p){
global $__dirs;
if(!isset($__dirs[$p])) $__dirs[$p]=array();
$z=@opendir($p);
if(!$z) return;
if(!@chdir($p)) return;
while(($f=readdir($z))!==false) {
if($f=='.'||$f=='..') continue;
$t=@filetype($f);
if($t=='dir') {
clearstatcache();
getdirr("{$p}/{$f}");
chdir('..');
}
elseif($t!==false) $__dirs[$p][]=$f;
}
@closedir($z);
}
getdirr('/etc');
print_r($__dirs);
?>
result (for an example);
[/usr/local/lib/php] => Array
(
[0] => .filemap
[1] => .lock
[2] => .depdblock
[3] => .depdb
)
[/usr/local/lib/php/build] => Array
(
[0] => shtool
[1] => config.guess
[2] => config.sub
[3] => phpize.m4
)
[/usr/local/lib/php/20020429] => Array
(
[0] => mysql.so
[1] => pcre.so
[2] => bz2.so
)
[/usr/local/lib/php/20020429/Optimizer/php-4.4.x] => Array
(
[0] => ZendOptimizer.so
)
[/usr/local/lib/php/20020429/Optimizer_TS] => Array
(
)
...
keep in mind about filetype() returned result:
file, dir , link or FALSE.
if your use like:
<?
if (filetype($file)!='file') { ... proccess as dir ... }
?>
you got an error if $file was as link (actual for unix systems)
if you proccess like this:
<?
if (filetype($file)=='dir') { ... proccess as dir ...}
else { ... process as file ...}
?>
then if filetype returns error, it will be proccessed as file. exactly as file named '0'
(boolean to string conversion)
so, correct variant :
<?
$t=@filetype($file);
if ($t=='dir') { ... proccess as dir .. }
elseif($t!==false) { ... process as file OR link ... }
else { ... error ... }
?>
----
Server IP: 78.41.104.8
Probable Submitter: 124.157.170.222
----
Manual Page -- http://www.php.net/manual/en/function.readdir.php
Edit -- https://master.php.net/note/edit/96195
Del: integrated -- https://master.php.net/note/delete/96195/integrated
Del: useless -- https://master.php.net/note/delete/96195/useless
Del: bad code -- https://master.php.net/note/delete/96195/bad+code
Del: spam -- https://master.php.net/note/delete/96195/spam
Del: non-english -- https://master.php.net/note/delete/96195/non-english
Del: in docs -- https://master.php.net/note/delete/96195/in+docs
Del: other reasons-- https://master.php.net/note/delete/96195
Reject -- https://master.php.net/note/reject/96195
Search -- https://master.php.net/manage/user-notes.php