PHP 8.4.0 RC3 available for testing

ftp_nb_put

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

ftp_nb_putArmazena um arquivo no servidor FTP (sem bloqueio)

Descrição

ftp_nb_put(
    FTP\Connection $ftp,
    string $remote_filename,
    string $local_filename,
    int $mode = FTP_BINARY,
    int $offset = 0
): int|false

ftp_nb_put() armazena um arquivo local no servidor FTP.

A diferença entre esta função e ftp_put() é que esta função envia o arquivo de forma assíncrona, para que a aplicação possa realizar outras operações enquanto o arquivo está sendo enviado.

Parâmetros

ftp

Uma instância de FTP\Connection.

remote_filename

O caminho do arquivo remoto.

local_filename

O caminho do arquivo local.

mode

O modo de transferência. Deve ser FTP_ASCII ou FTP_BINARY.

offset

A posição no arquivo remoto para iniciar o envio.

Valor Retornado

Retorna FTP_FAILED ou FTP_FINISHED ou FTP_MOREDATA, ou false em caso de falha ao abrir o arquivo local.

Registro de Alterações

Versão Descrição
8.1.0 O parâmetro ftp agora espera uma instância de FTP\Connection; anteriormente, um resource era esperado.
7.3.0 O parâmetro mode agora é opcional. Antigamente era obrigatório.

Exemplos

Exemplo #1 Exemplo de ftp_nb_put()

<?php

// Inicia o envio
$ret = ftp_nb_put($ftp, "test.remote", "test.local", FTP_BINARY);
while (
$ret == FTP_MOREDATA) {

// Faz alguma coisa
echo ".";

// Continua o envio...
$ret = ftp_nb_continue($ftp);
}
if (
$ret != FTP_FINISHED) {
echo
"Houve um erro ao enviar o arquivo...";
exit(
1);
}
?>

Exemplo #2 Continuando um envio com ftp_nb_put()

<?php

// Inicia
$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) {

// Faz alguma coisa
echo ".";

// Continua o envio...
$ret = ftp_nb_continue($ftp);
}
if (
$ret != FTP_FINISHED) {
echo
"Houve um erro ao enviar o arquivo...";
exit(
1);
}
?>

Veja Também

  • ftp_nb_fput() - Armazena um arquivo de um arquivo aberto no servidor FTP (sem bloqueio)
  • ftp_nb_continue() - Continua a receber/enviar um arquivo (sem bloquear)
  • ftp_put() - Envia um arquivo para o servidor FTP
  • ftp_fput() - Envia um arquivo aberto para o servidor FTP

adicione uma nota

Notas Enviadas por Usuários (em inglês) 6 notes

up
3
ted at hostleft dot com
19 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
3
manu at manux dot org
19 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
18 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
15 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
-1
kaiohken1982 at hotmail dot com
17 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
18 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.
To Top