PHP ftp_nb_fget() 函式
示例
從 FTP 伺服器下載檔案,並儲存到已開啟的本地檔案中(非阻塞)
<?php
// 連線並登入到 FTP 伺服器
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("無法連線到 $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$server_file = "somefile.txt";
// 開啟本地檔案進行寫入
$local_file = "local.txt";
$fp = fopen($local_file,"w");
// 發起下載
$d = ftp_nb_fget($ftp_conn, $fp, $server_file, FTP_BINARY)
while ($d == FTP_MOREDATA)
{
// 做任何你想做的事情
// 繼續下載
$d = ftp_nb_continue($ftp_conn);
}
if ($d != FTP_FINISHED)
{
echo "下載 $server_file 時出錯";
exit(1);
}
// 關閉連線和檔案控制代碼
ftp_close($ftp_conn);
fclose($fp);
?>
定義和用法
ftp_nb_fget() 函式從 FTP 伺服器獲取(下載)檔案,並將其儲存到已開啟的本地檔案中(非阻塞)。
提示:此函式(與 ftp_fget() 相反)非同步檢索檔案,因此您可以在檔案下載過程中執行其他操作。
語法
ftp_nb_fget(ftp_conn, open_file, server_file, mode, startpos);
引數值
引數 | 描述 |
---|---|
ftp_conn | 必需。指定要使用的 FTP 連線 |
open_file | 必需。指定一個已開啟的本地檔案,我們將資料儲存在其中 |
server_file | 必需。指定要下載的伺服器檔案 |
mode | 可選。指定傳輸模式。可能的值:FTP_ASCII 或 FTP_BINARY |
startpos | 可選。指定從遠端檔案中的哪個位置開始下載 |
技術詳情
返回值 | 以下值之一
|
---|---|
PHP 版本 | 4.3+ |
PHP 更新日誌 | PHP 7.3 - *mode* 引數已設為可選。 |
❮ PHP FTP 參考