Connecting Tech Pros Worldwide Help | Site Map

Dose PHP support recursive call? Walk through all subdir

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 01:30 PM
RC
Guest
 
Posts: n/a
Default Dose PHP support recursive call? Walk through all subdir

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!

  #2  
Old July 17th, 2005, 01:30 PM
RC
Guest
 
Posts: n/a
Default 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.
  #3  
Old July 17th, 2005, 01:30 PM
Daniel Tryba
Guest
 
Posts: n/a
Default 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.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.