略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: ftp_nb_put

2024-11-14

ftp_nb_put

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

ftp_nb_put存储一个文件至 FTP 服务器(non-blocking)

说明

ftp_nb_put(
    resource $ftp_stream,
    string $remote_file,
    string $local_file,
    int $mode = FTP_BINARY,
    int $startpos = 0
): int

ftp_nb_put() 函数用来把本地文件 local_file 存储到 FTP 服务器上由 remote_file 参数指定的路径。

与函数 ftp_put() 不同的是,此函数上传文件的时候采用的是异步传输模式,也就意味着在文件传送的过程中,你的程序可以继续干其它的事情。

参数

ftp_stream

FTP 连接的链接标识符。

remote_file

远程文件路径。

local_file

本地文件路径。

mode

传输模式选择,可选参数为 FTP_ASCII(文本模式)或 FTP_BINARY(二进制模式)。

startpos

指定传输开始的位置,用来续传支持。

返回值

返回 FTP_FAILEDFTP_FINISHEDFTP_MOREDATA

更新日志

版本 说明
7.3.0 mode 参数为可选,之前版本中为必填。

范例

示例 #1 ftp_nb_put() 示例

<?php

// 初始化
$ret ftp_nb_put($my_connection"test.remote""test.local"FTP_BINARY);
while (
$ret == FTP_MOREDATA) {
   
   
// 可以同时干其它事
   
echo ".";

   
// 继续上传...
   
$ret ftp_nb_continue($my_connection);
}
if (
$ret != FTP_FINISHED) {
   echo 
"上传过程中发生错误...";
   exit(
1);
}
?>

示例 #2 使用 ftp_nb_put() 来续传文件

<?php

// 初始化
$ret ftp_nb_put($my_connection"test.remote""test.local"
                      
FTP_BINARYftp_size("test.remote"));
// 另一种写法: $ret = ftp_nb_put($my_connection, "test.remote", "test.local", 
//                           FTP_BINARY, FTP_AUTORESUME);

while ($ret == FTP_MOREDATA) {
   
   
// 可以同时干其它事情
   
echo ".";

   
// 继续上传...
   
$ret ftp_nb_continue($my_connection);
}
if (
$ret != FTP_FINISHED) {
   echo 
"上传过程中发生错误...";
   exit(
1);
}
?>

参见

  • ftp_nb_fput() - 将文件存储到 FTP 服务器 (非阻塞)
  • ftp_nb_continue() - 连续获取/发送文件(以不分块的方式 non-blocking)
  • ftp_put() - 上传文件到 FTP 服务器
  • ftp_fput() - 上传一个已经打开的文件到 FTP 服务器
add a noteadd a note

User Contributed Notes 7 notes

up
3
manu at manux dot org
17 years ago
When using non blocking functions if you try to disconnect while your non blocking operation is in progress the disconnect command will not work until the operation is not finished.
up
3
Ariel asphp at dsgml dot com
15 years ago
Don't add a sleep() inside the loop. If you do you will severely slow down the upload.

In my tests, each time through the loop it send about 2.5K, looping about 220 times per second. (Which is very little.)

You won't necessarily get the same numbers as me per loop, but clearly PHP does it's own management of the loop so that you don't consume all the CPU on the server.
up
1
WebSee.ru
12 years ago
How to realize the possibility of transferring data from one FTP-server to another via FXP:

<?php
// ...

$ansver = ftp_raw($ftp_conn1, 'PASV');

if (
intval($ansver[0]) == 227) {
   
ftp_raw($ftp_conn2, 'PORT '.substr($ansver[0], $n = strpos($ansver[0], '(') + 1, strpos($m[0], ')', $n) - $n));
   
ftp_raw($ftp_conn1, 'STOR '.$filename); // need asynchronously (non-blocking)
   
ftp_raw($ftp_conn2, 'RETR '.$filename);
}
?>
up
2
ted at hostleft dot com
17 years ago
If you receive an error like: 

Warning:  ftp_nb_put(): Unable to service PORT commands in /path/to/file.php on line 27

verify whether you need to be in PASV mode. You can go into PASV mode by declaring

>  ftp_pasv($cnx,TRUE);
up
-1
kaiohken1982 at hotmail dot com
15 years ago
Hi,
I tried to use both ftp_put() and ftp_nb_put() adding the
variable $start = date("Y:m:d h:i:s"); at the begin of the script and the variable $end = date("Y:m:d h:i:s"); at its end, after the file upload function.
With the gprs connection I'm now using and trying to upload a .jpg file of 67,5 kb the time difference between $start and $end was 40 seconds in both cases, so I can suppose that there is no difference between these upload function.
The difference comes if you put anything inside the while ($ftp_upload == FTP_MOREDATA) loop.
I hope this note can help.
Regards
up
-1
brandon dot farber at gmail dot com
16 years ago
I couldn't see this noted anywhere...

ftp_nb_put apparently takes a much much longer time to upload the file than ftp_put (I haven't done any packet sniffing or logging tests to find out why).  I was using a script, nearly identical to the example above, and a 100KB file had only uploaded 3.99KB after about 8 minutes!  The php script naturally timed out before it was complete.

I changed my function to use ftp_put, got rid of the loop to check FTP_MOREDATA (as you will see in the example above), and the same script uploaded 2.2MB within 30 seconds with no other changes.

If you're using this function instead of ftp_put *purely to try to speed up your script* and it's taking a long time, you might want to try ftp_put instead.
up
-5
parasc at chetu dot com
3 years ago
Hi Everybody,
ftp_put not working in client server, but it working properly on my local  system.
Issue on client server:
Production.ERROR: ftp_put(): I won't open a connection to 172.31.17.181 (only to 52.33.186.63).

My script upload a file from local system to remote server.

Thanks
Paras Chauhan

官方地址:https://www.php.net/manual/en/function.ftp-nb-put.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3