473,241 Members | 1,490 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Dose PHP support recursive call? Walk through all subdir

RC
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!
Jul 17 '05 #1
2 1950
RC
RC wrote:
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);
}
?>


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.
Jul 17 '05 #2
RC <ra**********@nospam.noaa.gov> wrote:
OK, I re-write the function, it is work, now.
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...
while (($file = readdir($dirPointer)) != false) {


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.

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: TaeKyon | last post by:
I'm a python newbie; here are a few questions relative to a problem I'm trying to solve; I'm wandering if python is the best instrument or if awk or a mix of bash and sed would be better: 1) how...
19
by: Carlos Ribeiro | last post by:
Hello all, Here I am using some deeply nested, tree-like data structures. In some situations I need to traverse the tree; the old-style way to do it is to write a recursive method on the node...
8
by: Ryan Stewart | last post by:
Putting the following code in a page seems to make it go into an infinite loop unless you give it a very simple node to work with. Either that or it's very very slow. I'm somewhat new to this,...
1
by: Torsten Bronger | last post by:
Hallöchen! The following code from subprocess import call call() yields cp: call of stat for "subdir/*.jpg" not possible: File or directory not found
5
by: Jandre | last post by:
Hi I am a python novice and I am trying to write a python script (most of the code is borrowed) to Zip a directory containing some other directories and files. The script zips all the files fine...
2
by: Martin Marcher | last post by:
Hello, I'm playing around with os.walk and I made up del_tree(path) which I think is correct (in terms of the algorithm, but not as python wants it :)). As soon as some directory is deleted...
2
by: dj | last post by:
Hello All, I am attempting to us os.walk to populate two lists with values from a directory. The first list contains all the files in the directory and subdirectories. The second list contains...
6
by: cplusplusquestion | last post by:
As discussion last time, I am thinking if it is possible to change the recursive function from: void fun(Array a){ for (int i=0; i<MAX1; i++) for(int j=0; j<MAX2; j++){ if ( a != -1) { /*......
0
by: Fredrik Lundh | last post by:
A. Joseph wrote: os.walk traverses the directory tree, so I'm not sure why you think that your program needs to use recursion? wouldn't a plain loop work? import os, shutil for dirpath,...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.