Firstly I would like to say that this is NOT a request for help, but
rather the answer I found to the question I had asked more then a week
ago, but got no response.  From the lack of response I would assume that
a) nobody cared that I had a problem, or b) that nobody knew the
answer... I hope there were many less a)'s then b)'s but I thank
everyone who did take the time to at least read my request for help. I'm
posting what i found for the people who do their best to help, but don't
always know all of the answers (hey, who does?!)

My problem was with accessing the information awaiting me in sockets

heres the quick and dirty (the rest is after the code)

<?php
        // NOTE: THIS IS A WORKING SCRIPT YOU CAN TEST

        function display($data) {
                // used to echo our results...
                echo '<hr><pre>';
                if ( is_array($data) ) {
                        // make sure the data is real ;)
                        foreach ( $data as $idx => $val ) {
                                echo ':'.$idx.': '.$val;
                        }
                }
                echo '</pre><hr>';
        }

        function return_buffer($fp) {
                // get all the data from the socket buffer
                while ( $data = fgets($fp, 9999) ) {
                        $return[]=$data;
                }
                return($return);
        }

        // lets START establishing a connection to an FTP site...

        $host='ftp.cdrom.com';;
        $port=21;
        $user='anonymous';
        $pass='[EMAIL PROTECTED]';

        // actually initiate the connection
        $fp=fsockopen($host, $port, $est, $eno);
        socket_set_blocking($fp, 1);    // make fgets() wait for data
        socket_set_timeout($fp, 1, 0);  // set fgets() timeout delay

        $banner=return_buffer($fp);

        // send username
        fputs($fp, 'user '.$user.chr(10));
        // get response
        $user_resp=return_buffer($fp);

        // send password
        fputs($fp, 'pass '.$pass.chr(10));
        // get response
        $pass_resp=return_buffer($fp);

        // log off
        fputs($fp, 'quit'.chr(10).'bye'.chr(10));
        // get response (usually nothing)
        $quit_resp=return_buffer($fp);

        // close the socket (if it's still open)
        fclose($fp);

        // echo our data
        display($banner);
        display($user_resp);
        display($pass_resp);
        display($quit_resp);

?>

The above code DOES work, to some extent... ONLY the banner will be
received and echoed.... after much research, and brute force testing i
found that php keeps the timeout status on the socket.  which causes all
subsequent fgets() functions to do nothing at all (basically php
considerer's the socket to be at its EOF)  you can remedy this by
placing another "socket_set_timeout($fp, 1, 0);" in the script, but just
after the while loop in the return_buffer function... this will reset
the timeout status on the socket each time it's read from... the
resulting function (coded below) will allow the script to do precisely
as it was intended to do... log in, get all the data associated with
logging in, and then logging out...

        function return_buffer($fp) {
                // get all the data from the socket buffer
                while ( $data = fgets($fp, 9999) ) {
                        $return[]=$data;
                }
                socket_set_timeout($fp, 1, 0);
                return($return);
        }


Thanks again, and Cheers!
Replies, comments, concerns welcome



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to