2012-05-16 17 views
0

我试图通过使用PHP和Codeigniter的FTP发送文件。我实际上没有使用Codeigniter FTP类,因为它没有做我所需要的,所以这是本地PHP。通过PHP和Codeigniter获取FTP超时错误

基本上我需要的是脚本如果文件发送超时执行操作。目前,我的代码是这样的:

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

} 

所以基本上脚本连接到服务器,并试图将文件拖放在它目前的行动,如果连接不能建立,或者如果该文件无法删除。但我认为超时它只是在那里没有回应。我需要一个响应,因为它在自动化脚本中运行,并且用户知道发生了什么的唯一方法是脚本告诉他们。

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

任何帮助最赞赏:)

回答

0

如果你读the manual,省略了超时值默认为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()将抛出一个警告,如果登录credientals是错误的,所以你可能会处理用另外的方式(是错误处理或简单地supressing警告)。