From:             [EMAIL PROTECTED]
Operating system: WINDOWS NT
PHP version:      4.0.6
PHP Bug Type:     Reproducible crash
Bug description:  Problem with recursive function - only works if I echo something in 
it

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);


-- 
Edit bug report at: http://bugs.php.net/?id=12198&edit=1


-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to