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);
(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
ftp_nb_put — Envoie un fichier sur un serveur FTP (non-bloquant)
$ftp
,$remote_filename
,$local_filename
,$mode
= FTP_BINARY
,$offset
= 0
ftp_nb_put() écrit le fichier remote_filename
présent sur la machine locale, sur le serveur FTP ftp
.
La différence entre cette fonction et ftp_put() est que cette fonction peut lire le fichier de manière asynchrone, afin que votre programme fasse autre chose pendant que le fichier soit téléchargé.
ftp
Une instance de FTP\Connection.
remote_filename
Le chemin vers le fichier distant.
local_filename
Le chemin vers le fichier local.
mode
Le mode de transfert. Doit être soit FTP_ASCII
, soit
FTP_BINARY
.
offset
La position dans le fichier distant à partir de laquelle le téléchargement commencera.
Retourne FTP_FAILED
, FTP_FINISHED
ou FTP_MOREDATA
, ou false
en cas d'échec de l'ouverture du fichier local.
Version | Description |
---|---|
8.1.0 |
La paramètre ftp attend désormais une instance de
FTP\Connection ; auparavant, une ressource était attendu.
|
7.3.0 |
Le paramètre mode est maintenant optionel. Précédemment il
était obligatoire.
|
Exemple #1 Exemple avec ftp_nb_put()
<?php
// Initialisation du chargement
$ret = ftp_nb_put($ftp, "test.remote", "test.local", FTP_BINARY);
while ($ret == FTP_MOREDATA) {
// Faites ce que vous voulez...
echo ".";
// Continue le chargement...
$ret = ftp_nb_continue($ftp);
}
if ($ret != FTP_FINISHED) {
echo "Il y a eu un problème lors du chargement du fichier...";
exit(1);
}
?>
Exemple #2 Reprise d'un chargement avec ftp_nb_put()
<?php
// Initialisation
$ret = ftp_nb_put($ftp, "test.remote", "test.local",
FTP_BINARY, ftp_size("test.remote"));
// Ou : $ret = ftp_nb_put($ftp, "test.remote", "test.local",
// FTP_BINARY, FTP_AUTORESUME);
while ($ret == FTP_MOREDATA) {
// Faites ce que vous voulez...
echo ".";
// Continue le chargement...
$ret = ftp_nb_continue($ftp);
}
if ($ret != FTP_FINISHED) {
echo "Il y a eu un problème lors du chargement...";
exit(1);
}
?>
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);
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.
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.
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);
}
?>
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
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.