ftp_nb_get
(PHP 4 >= 4.3.0)
ftp_nb_get -- 重新得到一个 FTP 服务器上的文件并写入本地文件 (non-blocking)
描述
bool
ftp_nb_get ( resource ftp_stream, string local_file, string remote_file, int mode [, int resumepos])
ftp_nb_get() 函数用来重新得到参数 remote_file
指定的的远程文件,并保存到由参数 local_file
指定的本地文件。传输模式参数
mode 只能为 FTP_ASCII (文本模式) 或 FTP_BINARY
(二进制模式) 两种。与 ftp_get() 函数不同的是,此函数上传文件的时候采用的是异步传输模式,也就意味着在文件传送的过程中,你的程序可以继续干其它的事情。
如果成功则返回 TRUE,失败则返回 FALSE。
例子 1. ftp_nb_get() 实例 // 开始下载
$ret = ftp_nb_get($my_connection, "test", "README", FTP_BINARY);
while ($ret == FTP_MOREDATA) {
// 这里可以插入其它代码
echo ".";
// 继续下载...
$ret = ftp_nb_continue ($my_connection);
}
if ($ret != FTP_FINISHED) {
echo "下载中出错...";
exit(1);
} |
|
例子 2. 使用 ftp_nb_get() 函数恢复下载文件 // 开始
$ret = ftp_nb_get ($my_connection, "test", "README", FTP_BINARY,
filesize("test"));
// 或: $ret = ftp_nb_get ($my_connection, "test", "README",
// FTP_BINARY, FTP_AUTORESUME);
while ($ret == FTP_MOREDATA) {
// 可以插入其它代码
echo ".";
// 继续传送...
$ret = ftp_nb_continue ($my_connection);
}
if ($ret != FTP_FINISHED) {
echo "下载出错...";
exit(1);
} |
|
例子 3.
Resuming a download at position 100 to a new
file with ftp_nb_get()
// 禁止自动搜寻
ftp_set_option ($my_connection, FTP_AUTOSEEK, FALSE);
// 开始
$ret = ftp_nb_get ($my_connection, "newfile", "README", FTP_BINARY, 100);
while ($ret == FTP_MOREDATA) {
...
// 继续下载...
$ret = ftp_nb_continue ($my_connection);
} |
|
在上边的例子中,"newfile" 文件比服务器上的文件
"README" 要小 100 字节。这是因为我们是从文件的偏移量 100
处开始读取的,如果我们把参数 FTP_AUTOSEEK 禁止掉,则新文件的前 100
字节将会是 '\0'。
参考函数 ftp_nb_fget(),
ftp_nb_continue(),
ftp_get() and ftp_fget().