PHP 5.6.0beta1 released

ftp_nb_put

(PHP 4 >= 4.3.0, PHP 5)

ftp_nb_putSpeichert eine Datei auf dem FTP-Server (nicht-blockierend)

Beschreibung

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

ftp_nb_put() speichert eine lokale Datei auf dem FTP-Server.

Der Unterschied zwischen dieser Funktion und ftp_put() ist, dass diese die Datei asynchron hochlädt, so dass Ihr Programm noch andere Operationen durchführen kann während die Datei hochgeladen wird.

Parameter-Liste

ftp_stream

Der Verbindungshandler der FTP-Verbindung.

remote_file

Der Pfad zur Datei auf dem Server.

local_file

Der Pfad zur lokalen Datei.

mode

Der Transfer-Modus. Muss entweder FTP_ASCII oder FTP_BINARY sein.

startpos

Die Startposition für den Upload in der entfernten Datei.

Rückgabewerte

Gibt FTP_FAILED oder FTP_FINISHED oder FTP_MOREDATA zurück.

Beispiele

Beispiel #1 ftp_nb_put()-Beispiel

<?php

// Initialisiere den Upload
$ret ftp_nb_put($my_connection"test.remote""test.local"FTP_BINARY);
while (
$ret == FTP_MOREDATA) {

   
// Irgendwas machen
   
echo ".";

   
// Upload fortsetzen
   
$ret ftp_nb_continue($my_connection);
}
if (
$ret != FTP_FINISHED) {
   echo 
"Beim Transfer trat ein Fehler auf";
   exit(
1);
}
?>

Beispiel #2 Einen Upload mit ftp_nb_put() wieder aufnehmen

<?php

// Initialisieren
$ret ftp_nb_put($my_connection"test.remote""test.local",
                      
FTP_BINARYftp_size("test.remote"));
// ODER: $ret = ftp_nb_put($my_connection, "test.remote", "test.local",
// FTP_BINARY, FTP_AUTORESUME);


while ($ret == FTP_MOREDATA) {

   
// Irgendwas machen
   
echo ".";

   
// Upload fortsetzen
   
$ret ftp_nb_continue($my_connection);
}
if (
$ret != FTP_FINISHED) {
   echo 
"Beim Transfer trat ein Fehler auf";
   exit(
1);
}
?>

Siehe auch

  • ftp_nb_fput() - Speichert eine geöffnete Datei auf den FTP-Server (nicht blockierend)
  • ftp_nb_continue() - Nimmt die Übertragung einer Datei wieder auf (nicht-blockierend)
  • ftp_put() - Lädt eine Datei auf einen FTP-Server
  • ftp_fput() - Überträgt eine geöffnete Datei auf einen FTP-Server

add a note add a note

User Contributed Notes 6 notes

up
0
WebSee.ru
4 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
0
Ariel asphp at dsgml dot com
7 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
0
brandon dot farber at gmail dot com
8 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
0
ted at hostleft dot com
9 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
0
manu at manux dot org
9 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
-1
kaiohken1982 at hotmail dot com
7 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
To Top