ftp_nb_put

(PHP 4 >= 4.3.0, PHP 5)

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

说明

int ftp_nb_put ( resource ftp_stream, string remote_file, string local_file, int mode [, int startpos] )

ftp_nb_put() 函数用来把本地文件 local_file 存储到 FTP 服务器上由 remote_file 参数指定的路径。传输模式参数 mode 只能为 FTP_ASCII (文本模式) 或 FTP_BINARY (二进制模式) 两种。与函数 ftp_put() 不同的是,此函数上传文件的时候采用的是异步传输模式,也就意味着在文件传送的过程中,你的程序可以继续干其它的事情。

返回 FTP_FAILEDFTP_FINISHEDFTP_MOREDATA

例子 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_BINARY, ftp_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_nb_continue()ftp_put()ftp_fput()


add a note add a note User Contributed Notes
brandon dot farber at gmail dot com
02-Feb-2006 07:14
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.
ted at hostleft dot com
11-Jan-2005 05:13
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);
manu at manux dot org
08-Jan-2005 03:35
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.