函数名称:ftp_nb_fget()
函数描述:从 FTP 服务器上以非阻塞模式获取文件并写入本地文件
适用版本:PHP 4 >= 4.3.0, PHP 5, PHP 7
语法:ftp_nb_fget(resource $ftp_stream, resource $handle, string $remote_file, int $mode, int $resumepos = 0) : int
参数:
返回值:
示例:
// 连接到 FTP 服务器
$ftp_conn = ftp_connect('ftp.example.com');
if (!$ftp_conn) {
die('无法连接到 FTP 服务器');
}
// 登录到 FTP 服务器
$login_result = ftp_login($ftp_conn, 'username', 'password');
if (!$login_result) {
die('登录失败');
}
// 打开本地文件
$local_file = fopen('local_file.txt', 'w');
// 开始非阻塞模式获取文件
$remote_file = 'remote_file.txt';
$mode = FTP_ASCII;
$resumepos = 0;
$transfer_result = ftp_nb_fget($ftp_conn, $local_file, $remote_file, $mode, $resumepos);
// 检查传输状态
while ($transfer_result == FTP_MOREDATA) {
// 继续传输
$transfer_result = ftp_nb_continue($ftp_conn);
}
// 检查传输结果
if ($transfer_result == FTP_FINISHED) {
echo '文件传输已完成';
} else {
echo '文件传输失败';
}
// 关闭 FTP 连接和本地文件
ftp_close($ftp_conn);
fclose($local_file);
说明: