ftp_nb_put

(PHP 4 >= 4.3.0, PHP 5)

ftp_nb_put --  Загружает файл на FTP сервер в асинхронном режиме

Описание

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

ftp_nb_put() загружает локальный файл на FTP сервер.

Отличие этой функции от ftp_put() состоит в том, что загрузка файла происходит в асинхронном режиме, что позволяет программе выполнять другие операции во время загрузки.

Список параметров

ftp_stream

Идентификатор соединения с FTP сервером

remote_file

Путь к файлу на сервере

local_file

Путь к локальному файлу

mode

Режим передачи. Может принимать значения FTP_ASCII или FTP_BINARY

startpos

Позиция в файле, с которой начинается загрузка

Возвращаемые значения

Возвращает FTP_FAILED, FTP_FINISHED или FTP_MOREDATA.

Примеры

Пример 1. Пример использования ftp_nb_put() example

<?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_put
Ariel asphp at dsgml dot com
12-Jul-2006 12:11
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.
brandon dot farber at gmail dot com
01-Feb-2006 03: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 01: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
07-Jan-2005 11: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.

<ftp_nb_getftp_nlist>
 Last updated: Tue, 15 Nov 2005