คำอธิบาย
ฟังก์ชัน ftp_nb_fget() ดาวน์โหลดไฟล์จากเซิร์ฟเวอร์ FTP และบันทึกเข้าไปในไฟล์ที่เปิดอยู่ แบบ non-blocking
รูปแบบการใช้งาน (Syntax)
ftp_nb_fget($con, $open_file, $server_file, $transfer_mode, $beg_pos);
พารามิเตอร์
con − การเชื่อมต่อ FTP
open_file − ข้อมูลท้องถิ่นจะถูกเก็บที่นี่
server_file − ไฟล์เซิร์ฟเวอร์ที่จะดาวน์โหลด
transfer_mode − โหมดการถ่ายโอน ค่าที่เป็นไปได้:
FTP_ASCII, หรือ
FTP_BINARY
beg_pos − ตำแหน่งเริ่มต้นการดาวน์โหลด
ค่าที่คืนกลับ
ฟังก์ชัน ftp_nb_fget() คืนค่าอย่างใดอย่างหนึ่งต่อไปนี้:
FTP_FAILED − ส่ง/รับล้มเหลว
FTP_FINISHED − ส่ง/รับเสร็จสิ้น
FTP_MOREDATA − ส่ง/รับกำลังดำเนินการ
ตัวอย่าง
<?php
$ftp_server = "192.168.0.4";
$ftp_user = "jacob";
$ftp_pass = "tywg61gh";
$con = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($con, $ftp_user, $ftp_pass);
$my_serverfile = "demo.txt";
$local_file = "new.txt";
$file_pointer = fopen($local_file, "w");
$c = ftp_nb_fget($con, $file_pointer, $my_serverfile, FTP_BINARY);
while ($c == FTP_MOREDATA){
// ดาวน์โหลดต่อ
$c = ftp_nb_continue($con);
}
if ($c != FTP_FINISHED){
echo "Error downloading the server file!";
exit(1);
}
// ปิด
ftp_close($con);
fclose($file_pointer);
?>