ftp_nb_fput
(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
ftp_nb_fput — 将文件存储到 FTP 服务器 (非阻塞)
说明
ftp_nb_fput(
FTP\Connection
string
resource
int
int
): int
FTP\Connection
$ftp
,string
$remote_filename
,resource
$stream
,int
$mode
= FTP_BINARY
,int
$offset
= 0): int
ftp_nb_fput() 把已打开的文件内容存储到远程 FTP 服务器
本函数和 ftp_fput() 函数的区别是 本函数是异步上传文件。 所以在文件上传过程中,你的程序还可以执行其他操作。
参数
ftp
-
FTP\Connection 实例。
remote_filename
-
远程文件路径。
stream
-
已经打开的本地文件指针,当读取到文件末尾时结束。
mode
-
传输模式。必须是
FTP_ASCII
或FTP_BINARY
。 offset
-
要将文件存储到远程文件的开始位置(即从远程文件的哪个字节位置开始存储)。
返回值
返回 FTP_FAILED
或 FTP_FINISHED
或 FTP_MOREDATA
。
更新日志
版本 | 说明 |
---|---|
8.1.0 |
现在 ftp 参数接受 FTP\Connection
实例,之前接受 resource。
|
7.3.0 |
参数 mode 变为可选参数。
在之前的版本中,这是一个必填参数。
|
示例
示例 #1 ftp_nb_fput() 函数示例
<?php
$file = 'index.php';
$fp = fopen($file, 'r');
$ftp = ftp_connect($ftp_server);
$login_result = ftp_login($ftp, $ftp_user_name, $ftp_user_pass);
// 初始化上传
$ret = ftp_nb_fput($ftp, $file, $fp, FTP_BINARY);
while ($ret == FTP_MOREDATA) {
// 任何其他需要做的操作
echo ".";
// 继续上传...
$ret = ftp_nb_continue($ftp);
}
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 服务器
+添加备注
用户贡献的备注 2 notes
jascha at bluestatedigital dot com ¶
20 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.