ftp_nb_fget

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

ftp_nb_fgetLee un fichero en un servidor FTP y lo escribe en un fichero (no bloqueante)

Descripción

ftp_nb_fget(
    FTP\Connection $ftp,
    resource $stream,
    string $remote_filename,
    int $mode = FTP_BINARY,
    int $offset = 0
): int

ftp_nb_fget() lee el fichero remote_filename presente en el servidor FTP ftp.

La diferencia entre esta función y ftp_fget() es que esta función puede leer el fichero de manera asíncrona, de modo que su programa pueda realizar otras tareas mientras el fichero se descarga.

Parámetros

ftp

An FTP\Connection instance.

stream

Un puntero de fichero abierto en el que se escriben los datos.

remote_filename

La ruta hacia el fichero remoto.

mode

El modo de transferencia. Debe ser FTP_ASCII o FTP_BINARY.

offset

La posición en el fichero remoto desde la cual debe comenzar la descarga.

Valores devueltos

Devuelve FTP_FAILED o FTP_FINISHED o FTP_MOREDATA.

Historial de cambios

Versión Descripción
8.1.0 The ftp parameter expects an FTP\Connection instance now; previously, a recurso was expected.
7.3.0 El argumento mode ahora es opcional. Anteriormente era obligatorio.

Ejemplos

Ejemplo #1 Ejemplo con ftp_nb_fget()

<?php

// Apertura de algunos ficheros para escritura
$file = 'index.php';
$fp = fopen($file, 'w');

$ftp = ftp_connect($ftp_server);

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

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

// Realice lo que desee...
echo ".";

// Continúa la descarga...
$ret = ftp_nb_continue($ftp);
}
if (
$ret != FTP_FINISHED) {
echo
"Ocurrió un error durante la descarga del fichero...";
exit(
1);
}

// Cierra el puntero de fichero
fclose($fp);
?>

Ver también

  • ftp_nb_get() - Lee un fichero en un servidor FTP y lo escribe en un fichero (no bloqueante)
  • ftp_nb_continue() - Reanuda la descarga de un fichero (no bloqueante)
  • ftp_fget() - Descarga un fichero a través de FTP en un fichero local
  • ftp_get() - Descarga un fichero desde un servidor FTP

add a note

User Contributed Notes 1 note

up
5
pilif at pilif dot ch
20 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