Time out not working as expected

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CJdamaster
Forum Newbie
Posts: 2
Joined: Wed Nov 10, 2010 2:04 pm

Time out not working as expected

Post by CJdamaster »

Hi there, I need to update a SQL database from a remote Excel file (don't ask) which finds the file if the SQL database is out of date as a user goes on a web page requiring news data.
Updating only takes a few seconds provided the remote computer is switched on, so unless there is a much better way of updating rather than requiring a user to visit a page so it can update, I have this problem:

If the master computer is not on, obviously it cant connect to it, and therefore should give up. Now, for some reason, my 5 second time limit kinda doesn't work:

Code: Select all

// Attempt FTP connection to master computer
    // Connect
    set_time_limit(5);
    $connection = ftp_connect('******************');
    // Login
    $login_result = ftp_login($connection, '****', '****'); 

    // Check open
    if ((!$connection) || (!$login_result)) {
            ftp_close($connection);
    } else {

        // 100 Continue
        
        // Retrive Excel file from FTP
        // Open temporary file for storage
        $tempfile = './sqlupdate/updatefile.xls';
        $handle = fopen($tempfile, 'w');
etc...
It spends about a minute doing this, and gives me the error I would expect:
Fatal error: Maximum execution time of 5 seconds exceeded in sqlupdater.php on line 9 (the initial connection)
Obviously it's not 5 seconds...

Any ideas?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Time out not working as expected

Post by requinix »

The time limit doesn't apply to PHP's functions - just your own code. It took a minute for the ftp_connect to return (failed, I guess) which is when PHP noticed the time limit passed.
Put the timeout in the function call instead.
CJdamaster
Forum Newbie
Posts: 2
Joined: Wed Nov 10, 2010 2:04 pm

Re: Time out not working as expected

Post by CJdamaster »

I've found an answer which seems to work well: I chose not to execute the script if the server can't open a socket with the master computer.

I'm now using this function:

Code: Select all

function status($site, $port) {
    $status = array(false, true);
    $fp = @fsockopen($site, $port, $errno, $errstr, 2);
    if (!$fp) {
        return $status[0];
    } else { 
        return $status[1];}
}
and putting it into my original code:

Code: Select all

if ( status('*************************', 21) ) {
    
        // Connect
        $connection = ftp_connect('*************************');
        // Login
        $login_result = ftp_login($connection, '*********', '********'); 

        // Check open
        if ((!$connection) || (!$login_result)) {
                ftp_close($connection);
        } else {

            // 100 Continue
            
            // Retrive Excel file from FTP
            // Open temporary file for storage
            $tempfile = './sqlupdate/updatefile.xls';
            $handle = fopen($tempfile, 'w');
etc...
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Time out not working as expected

Post by requinix »

Please, do us all a favor and use this instead:

Code: Select all

function status($site, $port) {
    $fp = @fsockopen($site, $port, $errno, $errstr, 2);
    if (!$fp) {
        return false;
    } else { 
        fclose($fp);
        return true;
    }
}
Post Reply