Dose PHP support recursive call? Walk through all subdir

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RC

    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("Doc uments/2005");

    function readSubdir($dir Path) {
    $dirPointer = opendir($dirPat h);
    while (($file = readdir($dirPoi nter)) != false) {
    if ($file == "." || $file == "..")
    continue;
    if (filetype($file ) == "dir") {
    # if (is_dir($file) == true) {
    # if (strcmp(filetyp e($file), "dir") == 0) {
    readSubdir($fil e);
    } else {
    echo "file is $file\n";
    }
    }
    closedir($dirPo inter);
    }
    ?>

    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(filety pe("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

    #2
    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("Doc uments/2005");
    >
    > function readSubdir($dir Path) {
    > $dirPointer = opendir($dirPat h);
    > while (($file = readdir($dirPoi nter)) != false) {
    > if ($file == "." || $file == "..")
    > continue;
    > if (filetype($file ) == "dir") {
    > # if (is_dir($file) == true) {
    > # if (strcmp(filetyp e($file), "dir") == 0) {
    > readSubdir($fil e);
    > } else {
    > echo "file is $file\n";
    > }
    > }
    > closedir($dirPo inter);
    > }
    > ?>
    >[/color]

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

    <?php
    readSubdir("Doc uments/2005");

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

    Both filetype() and is_dir() functions are working fine.

    Comment

    • Daniel Tryba

      #3
      Re: Dose PHP support recursive call? Walk through all subdir

      RC <raymond.chui@n ospam.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($dirPoi nter)) != 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.

      Comment

      Working...