0
votes

I'm trying to send files via FTP using PHP and Codeigniter. I'm actually not using the Codeigniter FTP class as it doesn't do what I need, so for this it's native PHP.

Basically what I need is for the script to do an action if the file sent times out. Currently my code is this:

// connect to the ftp server
$connection = ftp_connect($item_server);

// login to the ftp account
$login = ftp_login($connection, $item_username, $item_password);

// if the connection or account login failed, change status to failed
if (!$connection || !$login) 
    { 
        // do the connection failed action here
    }
else
    {

// set the destination for the file to be uploaded to
$destination = "./".$item_directory.$item_filename;

// set the source file to be sent
$source = "./assets/photos/highres/".$item_filename;

// upload the file to the ftp server
$upload = ftp_put($connection, $destination, $source, FTP_BINARY);

// if the upload failed, change the status to failed
if (!$upload) 
    {
        // do the file upload failed action here
    }
// fi the upload succeeded, change the status to sent and close the ftp connection
else 
{
    ftp_close($connection);
    // update the item's status as 'sent'
// do the completed action here
    }

}

So basically the script connects to a server and attempts to drop the file in. It currently does an action if the connection could not be established, or if the file could not be dropped in. But I think for timeout it just sits there giving no response. I need a response for everything as it's running in an automated script and the only way for the user to know what's going on is if the script tells them.

How can I get a response if the server times out?

Any help is most appreciated :)

1

1 Answers

0
votes

If you read the manual, omitting the timeout value it defaults to 90 seconds.

You could set this value to something more acceptable and verify the connection alone instead of verifying the connection AND the login at the same time.

// connect to the ftp server and timeout after 15 seconds if connection can't be established
$connection = ftp_connect($item_server, 21, 15);
if( ! $connection )
{
    exit('A connection could not be established');  
}

// login to the ftp account
if( ! ftp_login($connection, $item_username, $item_password) )
{
    exit('A connection was established, but the credientials seems to be wrong');   
}

Please note that ftp_login() will throw a warning if the login credientals were wrong, so you'd might handle that in another way (be error handling or simply supressing the warning).