php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #12198 Problem with recursive function - only works if I echo something in it
Submitted: 2001-07-17 05:16 UTC Modified: 2002-02-04 02:12 UTC
From: silvio at european-internet dot com Assigned:
Status: Not a bug Package: Reproducible crash
PHP Version: 4.0.6 OS: WINDOWS NT
Private report: No CVE-ID: None
 [2001-07-17 05:16 UTC] silvio at european-internet dot com
I've written this class to scan through a remote directory using FTP functions. A recursive function is used to do the job, but it only works if I print something to screen, else it hangs and the the script eventually times out.
Here's the code, notice the first line of function readDir():

class SiteScan {
    var $_sSite;
    var $_iPort;
    var $_sUsername;
    var $_sPassword;
    var $_bDebug;
    var $_aErrors;

    //ftp
    var $_iConnectionStream;
    var $_sSystemType;
    var $_aFileStructure;


    function SiteScan($site='localhost',$u='Administrator',$p='',$port=21) {
        //init connection
        $this->_iPort = $port;
        $this->_sSite = $site;
        $this->_sUsername = $u;
        $this->_sPassword = $p;

        $this->_bDebug = true;
        $this->_aErrors = array();

        //try to obtein an ftp stream
        if ($stream = ftp_connect($this->_sSite,$this->_iPort)) {
            $this->_iConnectionStream = $stream;

            //FileSystem successfully determined, now try to log in
            if (!ftp_login($this->_iConnectionStream,$this->_sUsername,$this->_sPassword)) {
                $this->_aErrors[] = '<b>ERROR 002:</b> Username ' .$this->_sUsername. ' and password ' .$this->_sPassword. ' rejected';
            } else {
                //connection successful! Now try to determine operating system
                if (!$this->_sSystemType = ftp_systype($stream)) {
                    $this->_aErrors[] = '<b>ERROR 003:</b> File system type could not be determined';
                }
            }

        } else {
            $this->_aErrors[] = '<b>ERROR 001:</b> Could not connect to ' . $this->_sSite . ':' . $this->_iPort;
        }

        if ($this->_bDebug) {
            $this->_onError();
        }
    }

    function _analyseDir($aDirLine) {
        $aDirInfo = array();
        if(ereg("([-dl])[rwxst-]{9}",substr($aDirLine,0,10))) {
          $this->_sSystemType = "UNIX";
        }

        if (substr($aDirLine,0,5) == "total") {
            $aDirInfo[0] = -1;
        } elseif ($this->_sSystemType == "Windows_NT") {

            if (ereg("[-0-9]+ *[0-9:]+[PA]?M? +<DIR> {10}(.*)",$aDirLine,$aRegExp)) {
                $aDirInfo[0] = 1;
                $aDirInfo[1] = 0;
                $aDirInfo[2] = $aRegExp[1];
            } elseif(ereg("[-0-9]+ *[0-9:]+[PA]?M? +([0-9]+) (.*)",$aDirLine,$aRegExp)) {
                $aDirInfo[0] = 0;
                $aDirInfo[1] = $aRegExp[1];
                $aDirInfo[2] = $aRegExp[2];
            }
        } elseif ($this->_sSystemType == "UNIX") {
            if (ereg("([-d])[rwxst-]{9}.* ([0-9]*) [a-zA-Z]+ [0-9: ]*[0-9] (.+)",$aDirLine,$aRegExp)) {
                if ($aRegExp[1]=="d") $aDirInfo[0] = 1;
                    $aDirInfo[1] = $aRegExp[2];
                    $aDirInfo[2] = $aRegExp[3];
                }
            }

            if (($aDirInfo[2] == ".") || ($aDirInfo[2] == "..")) {
                $aDirInfo[0] = 0;
            }

            return $aDirInfo;
    }

    function readDir($sDir) {
        //if this echo statement is removed, then the whole script hangs!!!
        echo ' ';

        //get an array of files from the base directory
        $aDirList = ftp_rawlist($this->_iConnectionStream,"");

        for ($iCount=0;$iCount<count($aDirList);$iCount++) {
             $aDirInfo = $this->_analyseDir($aDirList[$iCount]);

             //if current file is a directory
             if ($aDirInfo[0] == 1) {
                 $sNewDir = "$sDir/$aDirInfo[2]";

                 if (($aDirInfo[2] == "~") || (substr($aDirInfo[2],0,1) == " ")) {
                      $chdir=ftp_chdir($this->_iConnectionStream,$sNewDir);
                 } else {
                      $chdir=ftp_chdir($this->_iConnectionStream,$aDirInfo[2]);
                 }

                 $stop = 0;

                 if (!$stop && $chdir) {
                      $this->readDir($sNewDir);
                 }

                 if (!ftp_chdir($this->_iConnectionStream,$sDir)) {
                     ftp_cdup($this->_iConnectionStream);
                 }

             } elseif ($aDirInfo[0]==0) {
                 $this->_aFileStructure[$sDir][] = $aDirInfo[2];
                 $size += $aDirInfo[1];
             }
        }
    }

    function _onError() {
        //
        if (count($this->_aErrors) > 0) {
            echo '<div id="debug" style="position:absolute; left:25px; top:30px; width:575px; height:15px; z-index:1; background-color: #FFFFCC; layer-background-color: #FFFFCC; border: 1px none #000000">'."\n".'<pre>';
            while (list($iKey,$sError) = each($this->_aErrors)) {
                echo $sError."\n";
            }
            echo '</pre></div>';
        }
    }

    function ftpClose() {
        //
        ftp_quit($this->_iConnectionStream);
    }


}

//
echo '<pre>connecting to ftp site<br>';
$ftp = new SiteScan('yoursite','username','password','port');
$ftp->readDir('');
$ftp->ftpClose();
print_r($ftp->_aFileStructure);


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-07-22 15:35 UTC] zeev@php.net
Are you sure that when you print out something, does the script run all the way and terminate properly?  Printing may just give you a 'sign of life', but it may time-out eventually still.
 [2002-02-04 02:12 UTC] yohgaki@php.net
The version of PHP that this bug was reported in is too old. Please
try to reproduce this bug in the latest version of PHP (available
from http://www.php.net/downloads.php

If you are still able to reproduce the bug with one of the latest
versions of PHP, please change the PHP version on this bug report
to the version you tested and change the status back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 08:01:30 2024 UTC