使用 PHP 和代码点火器超时时出现 FTP 错误


Getting FTP error for timeout with PHP & Codeigniter

我正在尝试使用 PHP 和 Codeigniter 通过 FTP 发送文件。我实际上没有使用Codeigniter FTP类,因为它没有做我需要的,所以它是本机PHP。

基本上我需要的是脚本在

发送的文件超时时执行操作。目前我的代码是这样的:

// 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
    }
}

所以基本上脚本连接到服务器并尝试将文件放入其中。如果无法建立连接或无法删除文件,它当前会执行操作。但我认为对于超时,它只是坐在那里没有回应。我需要对所有内容进行响应,因为它在自动脚本中运行,用户知道发生了什么的唯一方法是脚本是否告诉他们。

如果服务器超时,如何获得响应?

任何帮助都是最感激的:)

如果您阅读手册,省略超时值,则默认为 90 秒。

您可以将此值设置为更可接受的值并单独验证连接,而不是同时验证连接和登录名。

// 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');   
}

请注意,如果登录成分错误,ftp_login()会抛出警告,因此您可以通过另一种方式处理(错误处理或只是抑制警告)。