ftp_nb_fget

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

ftp_nb_fgetRecupera un archivo desde el servidor FTP y lo escribe en un archivo abierto (modo no-bloqueo)

Descripción

ftp_nb_fget(
    resource $ftp_stream,
    resource $handle,
    string $remote_file,
    int $mode,
    int $resumepos = 0
): int

ftp_nb_fget() recupera un archivo remoto desde el servidor FTP.

La diferencia entre esta función y ftp_fget() es que esta función recupera el archivo de forma asincrónica, de modo que su programa puede realizar otras operaciones mientras el archivo está siendo descargado.

Parámetros

ftp_stream

El identificador de enlace de la conexión FTP.

handle

Un apuntador de archivo abierto en el cual almacenar los datos.

remote_file

La ruta del archivo remoto.

mode

El modo de transferencia. Debe ser FTP_ASCII o FTP_BINARY.

resumepos

Posición en el fichero remoto en donde empezar la descarga

Valores devueltos

Devuelve FTP_FAILED o FTP_FINISHED o FTP_MOREDATA.

Ejemplos

Ejemplo #1 Ejemplo de ftp_nb_fget()

<?php

// abrir un archivo para lectura
$file = 'index.php';
$fp = fopen($file, 'w');

$conn_id = ftp_connect($ftp_server);

$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// Iniciar la descarga
$ret = ftp_nb_fget($conn_id, $fp, $file, FTP_BINARY);
while (
$ret == FTP_MOREDATA) {

// Haga lo que quiera
echo ".";

// Continuar la descarga ...
$ret = ftp_nb_continue($conn_id);
}
if (
$ret != FTP_FINISHED) {
echo
"Hubo un error al descargar el archivo...";
exit(
1);
}

// cerrar el apuntador de archivo
fclose($fp);
?>

Ver también

  • ftp_nb_get() - Recupera un archivo desde el servidor FTP y lo escribe en un archivo local (modo no-bloqueo)
  • ftp_nb_continue() - Continúa recuperando/enviando un archivo (modo no-bloqueo)
  • ftp_fget() - Descarga un archivo desde el servidor FTP y lo guarda en un archivo abierto
  • ftp_get() - Descarga un archivo desde el servidor FTP

add a note add a note

User Contributed Notes 1 note

up
5
pilif at pilif dot ch
19 years ago
If you want to monitor the progress of the download, you may use the filesize()-Function.

But note: The results of said function are cached, so you'll always get 0 bytes. Call clearstatcache() before calling filesize() to determine the actual size of the downloaded file.

This may have performance implications, but if you want to provide the information, there's no way around it.

Above sample extended:

<?php
// get the size of the remote file
$fs = ftp_size($my_connection, "test");

// Initate the download
$ret = ftp_nb_get($my_connection, "test", "README", FTP_BINARY);
while (
$ret == FTP_MOREDATA) {
  
  
clearstatcache(); // <- this is important
  
$dld = filesize($locfile);
   if (
$dld > 0 ){
      
// calculate percentage
      
$i = ($dld/$fs)*100;
      
printf("\r\t%d%% downloaded", $i);
   }  

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

Philip
To Top