2012-05-16 37 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警告)。