Connecting Tech Pros Worldwide Forums | Help | Site Map

Dose PHP support recursive call? Walk through all subdir

RC
Guest
 
Posts: n/a
#1: Jul 17 '05
I assume the answer is yes.

I try to write readSubdir function read all files
in all sub-directories.

<?php
readSubdir("Documents/2005");

function readSubdir($dirPath) {
$dirPointer = opendir($dirPath);
while (($file = readdir($dirPointer)) != false) {
if ($file == "." || $file == "..")
continue;
if (filetype($file) == "dir") {
# if (is_dir($file) == true) {
# if (strcmp(filetype($file), "dir") == 0) {
readSubdir($file);
} else {
echo "file is $file\n";
}
}
closedir($dirPointer);
}
?>

You see above I tried to used three different methods
to check a directory.

* 1st way I used filetype() function
I got errors

Warning: filetype(): Lstat failed for 05 in /MyPath/myreaddir.php on line 13
file is 05

Warning: filetype(): Lstat failed for 06 in /MyPath/myreaddir.php on line 13
file is 06

Where 05, 06 are sub-directories, it fails return a string "dir"! Why?!

2nd way I used is_dir() function, but it fails return true! Why?!

file is 05
file is 06


3rd way I used strcmp for return from file type, it gets the same
error as 1st way.


But when I tried to use

var_dump(is_dir("Documents/2005/05"));
var_dump(filetype("Documents/2005/05"));

I got the CORRECT display

bool(true)
string(3) "dir"

Can anyone out there help solve this mystery?
Thank Q very much in advance!

RC
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Dose PHP support recursive call? Walk through all subdir


RC wrote:[color=blue]
> I assume the answer is yes.
>
> I try to write readSubdir function read all files
> in all sub-directories.
>
> <?php
> readSubdir("Documents/2005");
>
> function readSubdir($dirPath) {
> $dirPointer = opendir($dirPath);
> while (($file = readdir($dirPointer)) != false) {
> if ($file == "." || $file == "..")
> continue;
> if (filetype($file) == "dir") {
> # if (is_dir($file) == true) {
> # if (strcmp(filetype($file), "dir") == 0) {
> readSubdir($file);
> } else {
> echo "file is $file\n";
> }
> }
> closedir($dirPointer);
> }
> ?>
>[/color]

OK, I re-write the function, it is work, now.

<?php
readSubdir("Documents/2005");

function readSubdir($dirPath) {
chdir($dirPath);
$dirPointer = opendir(".");
while (($file = readdir($dirPointer)) != false) {
if ($file == "." || $file == "..")
continue; // skip current and upper directories
if (filetype($file) == "dir") {
# if (is_dir($file) == true) {
readSubdir($file);
} else {
echo "file is $file\n";
}
}
closedir($dirPointer);
chdir("..");
}
?>

Both filetype() and is_dir() functions are working fine.
Daniel Tryba
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Dose PHP support recursive call? Walk through all subdir


RC <raymond.chui@nospam.noaa.gov> wrote:[color=blue]
> OK, I re-write the function, it is work, now.[/color]

So I guess you found out that you weren't setting the correct path?

Your new function might get you into trouble in case chdir() fails. You
could check if the directory you are about to readSubdir() to is
executable and readable... else there is no point to even go there.

Better yet it so fix your original code and append the found dir to the
$dirPath in the new readSubdir() call...
[color=blue]
> while (($file = readdir($dirPointer)) != false) {[/color]

Also this will get you into trouble for files called "0". Never ever use
!= if you know that an error results in a boolean false, always use
!==/=== when types are known.

Closed Thread