note 84638 added to function.scandir
| From: | webmaster at asylum-et dot com | Date: | Wed, 23 Jul 2008 07:50:27 +0000 |
| Subject: | note 84638 added to function.scandir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-142587@lists.php.net to get a copy of this message | ||
<?php
/**
* outputs all files and directories
* recursively starting with the given
* $base path. This function is a combination
* of some of the other snips on the php.net site.
* All are good but lacked one thing or another
* that I needed like .htaccess
* files get excluded with the one that checks to
* see if the first character is a . and omits
* that.
*
* @example rscandir(dirname(__FILE__).'/'));
* @param string $base
* @param array $omit
* @param array $data
* @return array
*/
function rscandir($base='', &$data=array()) {
$array = array_diff(scandir($base), array('.', '..')); # remove ' and ..
from the array */
foreach($array as $value) : /* loop through the array at the level of the supplied $base */
if (is_dir($base.$value)) : /* if this is a directory */
$data[] = $base.$value.'/'; /* add it to the $data array */
$data = rscandir($base.$value.'/', $data); /* then make a recursive call with the
current $value as the $base supplying the $data array to carry into the recursion */
elseif (is_file($base.$value)) : /* else if the current $value is a file */
$data[] = $base.$value; /* just add the current $value to the $data array */
endif;
endforeach;
return $data; // return the $data array
}
echo '<pre>'; var_export(rscandir(dirname(__FILE__).'/')); echo
'</pre>';
/*
Output:
array (
0 => '/path/to/web/.htaccess',
1 => '/path/to/web/foo/',
2 => '/path/to/web/foo/file.txt'
)
*/
?>
----
Server IP: 69.147.83.197
Probable Submitter: 75.68.207.69
----
Manual Page -- http://www.php.net/manual/en/function.scandir.php
Edit -- https://master.php.net/note/edit/84638
Del: integrated -- https://master.php.net/note/delete/84638/integrated
Del: useless -- https://master.php.net/note/delete/84638/useless
Del: bad code -- https://master.php.net/note/delete/84638/bad+code
Del: spam -- https://master.php.net/note/delete/84638/spam
Del: non-english -- https://master.php.net/note/delete/84638/non-english
Del: in docs -- https://master.php.net/note/delete/84638/in+docs
Del: other reasons-- https://master.php.net/note/delete/84638
Reject -- https://master.php.net/note/reject/84638
Search -- https://master.php.net/manage/user-notes.php