ftp_nb_fput
(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
ftp_nb_fput — 将文件存储到 FTP 服务器 (非阻塞)
说明
ftp_nb_fput(
resource
string
resource
int
int
): int
resource
$ftp_stream
,string
$remote_file
,resource
$handle
,int
$mode
= FTP_IMAGE
,int
$startpos
= 0): int
ftp_nb_fput() 把已打开的文件内容存储到远程 FTP 服务器
本函数和 ftp_fput() 函数的区别是 本函数是异步上传文件。 所以在文件上传过程中,你的程序还可以执行其他操作。
参数
-
ftp_stream
-
FTP 连接标示符。
-
remote_file
-
远程文件路径。
-
handle
-
已经打开的本地文件指针,当读取到文件末尾时结束。
-
mode
-
传输模式。必须是
FTP_ASCII
或FTP_BINARY
。 -
startpos
-
要将文件存储到远程文件的开始位置(即从远程文件的哪个字节位置开始存储)。
返回值
返回 FTP_FAILED
或 FTP_FINISHED
或 FTP_MOREDATA
。
更新日志
版本 | 说明 |
---|---|
7.3.0 |
参数 mode 变为可选参数。
在之前的版本中,这是一个必填参数。
|
范例
示例 #1 ftp_nb_fput() 函数例程
<?php
$file = 'index.php';
$fp = fopen($file, 'r');
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// 初始化上传
$ret = ftp_nb_fput($conn_id, $file, $fp, FTP_BINARY);
while ($ret == FTP_MOREDATA) {
// 任何其他需要做的操作
echo ".";
// 继续上传...
$ret = ftp_nb_continue($conn_id);
}
if ($ret != FTP_FINISHED) {
echo "There was an error uploading the file...";
exit(1);
}
fclose($fp);
?>
参见
- ftp_nb_put() - 存储一个文件至 FTP 服务器(non-blocking)
- ftp_nb_continue() - 连续获取/发送文件(以不分块的方式 non-blocking)
- ftp_put() - 上传文件到 FTP 服务器
- ftp_fput() - 上传一个已经打开的文件到 FTP 服务器

User Contributed Notes 2 notes
jascha at bluestatedigital dot com ¶
17 years ago
There is an easy way to check progress while uploading a file. Just use the ftell function to watch the position in the file handle. ftp_nb_fput will increment the position as the file is transferred.
Example:
<?
$fh = fopen ($file_name, "r");
$ret = ftp_nb_fput ($ftp, $file_name, $fh, FTP_BINARY);
while ($ret == FTP_MOREDATA) {
print ftell ($fh)."\n";
$ret = ftp_nb_continue($ftp);
}
if ($ret != FTP_FINISHED) {
print ("error uploading\n");
exit(1);
}
fclose($fh);
?>
This will print out the number of bytes transferred thus far, every time the loop runs. Coverting this into a percentage is simply a matter of dividing the number of bytes transferred by the total size of the file.