473,396 Members | 2,087 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,396 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 1952
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,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.