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 — Lit un fichier sur un serveur FTP, et l'écrit dans un fichier (non bloquant)
Description
$ftp_stream
, resource $handle
, string $remote_file
, int $mode
[, int $resumepos = 0
] )
ftp_nb_fget() lit le fichier remote_file
présent sur le serveur FTP ftp_stream.
La différence entre cette fonction et ftp_fget() est que cette fonction peut lire le fichier de manière asynchrone, afin que votre programme fasse autre chose pendant que le fichier est téléchargé.
Liste de paramètres
-
ftp_stream -
L'identifiant du lien de connexion FTP.
-
handle -
Un pointeur de fichier ouvert dans lequel les données sont écrites.
-
remote_file -
Le chemin vers le fichier distant.
-
mode -
Le mode de transfert. Doit être soir
FTP_ASCIIouFTP_BINARY. -
resumepos -
La position dans le fichier distant à partir de laquelle le téléchargement doit commencer.
Valeurs de retour
Retourne FTP_FAILED ou FTP_FINISHED
ou FTP_MOREDATA.
Exemples
Exemple #1 Exemple avec ftp_nb_fget()
<?php
// Ouverture de quelques fichiers pour lecture
$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);
// Initialise le téléchargement
$ret = ftp_nb_fget($conn_id, $fp, $file, FTP_BINARY);
while ($ret == FTP_MOREDATA) {
// Faites ce que vous voulez...
echo ".";
// Continue le téléchargement...
$ret = ftp_nb_continue($conn_id);
}
if ($ret != FTP_FINISHED) {
echo "Il y a eu une erreur pendant le téléchargement du fichier...";
exit(1);
}
// Ferme le pointeur de fichier
fclose($fp);
?>
Voir aussi
- ftp_nb_get() - Lit un fichier sur un serveur FTP, et l'écrit dans un fichier (non bloquant)
- ftp_nb_continue() - Reprend le téléchargement d'un fichier (non bloquant)
- ftp_fget() - Télécharge un fichier via FTP dans un fichier local
- ftp_get() - Télécharge un fichier depuis un serveur FTP
