note 50249 added to ref.dir
| From: | bardoartifex at yahoo dot com dot br | Date: | Wed, 23 Feb 2005 03:00:30 +0000 |
| Subject: | note 50249 added to ref.dir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85395@lists.php.net to get a copy of this message | ||
Hi, I worked a lot with some usefull recursive functions and one "C pointer-like" approach
using PHP. The result is a small code to read directories and files to generate an associative
array.
I hope if this will be usefull for you as was to me. (Ah, sorry by my poor english!)
There it's the code:
function &walkThroughDir ($path,&$filelist)
{
if (!is_dir($path)) return NULL;
$resdir = @opendir ($path) or die("Error reading ".$path);
// It's the secret for this recursive function
$ret = &$filelist;
while ( ($entry=readdir($resdir))!==false ) {
if ( is_dir($path."/".$entry)=="dir" ) { //if it's a
directory...
if ($entry!="." and $entry!="..") {//do not use ./ or ../
// Call himself using the same result array
walkThroughDir($path."/".$entry,$ret);
}//if
} else {
// if it is not a dir, read the filenames
$ret[$path][] = $entry;
}//if
}//while
closedir($resdir);
return $ret;
}//walkThroughDir
This function will generate something alike:
Array
(
[/Library/WebServer/Documents/component/test/subtest] => Array
(
[0] => linuxitscool.php
)
[/Library/WebServer/Documents/component/test] => Array
(
[0] => macosxrulez.php
[1] => unixrulez.php
)
[/Library/WebServer/Documents/dev/component] => Array
(
[0] => Component.h.php
[1] => Component.pkg.php
)
)
----
Manual Page -- http://www.php.net/manual/en/ref.dir.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50249
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50249&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50249&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50249&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50249&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50249&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50249&report=yes
Search -- http://master.php.net/manage/user-notes.php