ftp_nb_get

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

ftp_nb_getObtém um arquivo do servidor FTP e escreve-o em um arquivo local (sem bloquear)

Descrição

ftp_nb_get ( resource $ftp_stream , string $local_file , string $remote_file , int $mode , int $resumepos = ? ) : int

ftp_nb_get() obtém um arquivo remoto do servidor FTP e salva-o em um arquivo local.

A diferença entre esta função é ftp_get() é que esta função obtém o arquivo de forma assincronoma, assim o seu programa pode realizar outras operações enquanto o arquivo esta sendo baixado.

Parâmetros

ftp_stream

O identificador com a conexão FTP.

local_file

O caminho para o arquivo local (será sobrescrito se o arquivo local já existir).

remote_file

O caminho para o arquivo remoto.

mode

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

resumepos

Valor Retornado

Retorna FTP_FAILED ou FTP_FINISHED ou FTP_MOREDATA.

Exemplos

Exemplo #1 Exemplo ftp_nb_get()

<?php

// Initate the download
$ret ftp_nb_get($my_connection"test""README"FTP_BINARY);
while (
$ret == FTP_MOREDATA) {
   
   
// Do whatever you want
   
echo ".";

   
// Continue downloading...
   
$ret ftp_nb_continue($my_connection);
}
if (
$ret != FTP_FINISHED) {
   echo 
"There was an error downloading the file...";
   exit(
1);
}
?>

Exemplo #2 Continuando um download com ftp_nb_get()

<?php

// Initate 
$ret ftp_nb_get($my_connection"test""README"FTP_BINARY
                      
filesize("test"));
// OR: $ret = ftp_nb_get($my_connection, "test", "README", 
//                           FTP_BINARY, FTP_AUTORESUME);
while ($ret == FTP_MOREDATA) {
   
   
// Do whatever you want
   
echo ".";

   
// Continue downloading...
   
$ret ftp_nb_continue($my_connection);
}
if (
$ret != FTP_FINISHED) {
   echo 
"There was an error downloading the file...";
   exit(
1);
}
?>

Exemplo #3 Continuando um download na posição 100 para um novo arquivo com ftp_nb_get()

<?php

// Disable Autoseek
ftp_set_option($my_connectionFTP_AUTOSEEKfalse);

// Initiate
$ret ftp_nb_get($my_connection"newfile""README"FTP_BINARY100);
while (
$ret == FTP_MOREDATA) {

   
/* ... */
   
   // Continue downloading...
   
$ret ftp_nb_continue($my_connection);
}
?>

No exemplo acima, newfile é 100 bytes menor do que README no servidor FTP porque nós começamos a ler a partir da posição 100. Se nós não disabilitarmos FTP_AUTOSEEK, os primeiros 100 bytes de newfile serão '\0'.

Veja Também

  • ftp_nb_fget() - Obtém um arquivo de um servidor FTP e escreve-o para um arquivo aberto(sem bloquear)
  • ftp_nb_continue() - Continua a receber/enviar um arquivo (sem bloquear)
  • ftp_fget() - Copia um arquivo do servidor FTP e salva em um arquivo aberto
  • ftp_get() - Copia um arquivo do servidor FTP

add a note add a note

User Contributed Notes 1 note

up
0
passerbyxp at gmail dot com
12 years ago
Note that you may have to keep calling ftp_nb_continue in order to complete the download. For example, if you do this:

<?php
ftp_nb_get
($conn,$localfile,$remotefile,FTP_BINARY)
//do some LONG time work
while(ftp_nb_continue($conn)!=FTP_FINISHED){}
?>

Your local file may only contains a few kilobytes and the later ftp_nb_continue will keep raising warning of no more data (due to connection time out, I guess).

So you may want to do this instead:

<?php
$dl
=ftp_nb_get($conn,$localfile,$remotefile,FTP_BINARY)
//part of long time work
if(ftp_nb_continue($conn)==FTP_MOREDATA) {}
//part of long time work
if(ftp_nb_continue($conn)==FTP_MOREDATA) {}
//continue to do this until you finish the long time work
while(ftp_nb_continue($conn)==FTP_MOREDATA){}
?>

This happened on my Windows XP + PHP 5.3.8 under CLI. Hope this helps someone.
To Top