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
ftp_nb_fget
(PHP 4 >= 4.3.0, PHP 5)
ftp_nb_fget — Recupera un archivo desde el servidor FTP y lo escribe sobre un archivo abierto (modo no-bloqueo)
Descripción
ftp_nb_fget() recupera un archivo remoto desde el servidor FTP.
La diferencia entre esta función y ftp_fget() es que la presente función recupera el archivo de forma asincrónica, así que su programa puede realizar otras operaciones mientras que el archivo está siendo descargado.
Lista de parámetros
- secuencia_ftp
-
El identificador de enlace de la conexión FTP.
- gestor
-
Un apuntador de archivo abierto en el cual almacenar los datos.
- archivo_remoto
-
La ruta del archivo remoto.
- modo
-
El modo de transferencia. Debe ser FTP_ASCII o FTP_BINARY.
- pos_continuacion
Valores retornados
Devuelve FTP_FAILED o FTP_FINISHED o FTP_MOREDATA.
Ejemplos
Example #1 Ejemplo de ftp_nb_fget()
<?php
// abrir un archivo para lectura
$archivo = 'index.php';
$da = fopen($archivo, 'w');
$id_con = ftp_connect($servidor_ftp);
$resultado_login = ftp_login($id_con, $nombre_usuario_ftp, $contrasenya_ftp);
// Iniciar la descarga
$ret = ftp_nb_fget($id_con, $da, $archivo, FTP_BINARY);
while ($ret == FTP_MOREDATA) {
// Haga lo que desee
echo ".";
// Continuar la descarga...
$ret = ftp_nb_continue($id_con);
}
if ($ret != FTP_FINISHED) {
echo "Hubo un error en la descarga del archivo...";
exit(1);
}
// cerrar el apuntador de archivo
fclose($da);
?>
Ver también
- ftp_nb_get() - Recupera un archivo desde el servidor FTP y lo escribe sobre 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
ftp_nb_fget
16-Nov-2004 01:53
