PHP Doku:: Lädt eine Datei vom FTP-Server und schreibt sie in eine lokale Datei (nicht-blockierend) - function.ftp-nb-fget.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige DiensteFTPFTP-Funktionenftp_nb_fget

Ein Service von Reinhard Neidl - Webprogrammierung.

FTP-Funktionen

<<ftp_nb_continue

ftp_nb_fput>>

ftp_nb_fget

(PHP 4 >= 4.3.0, PHP 5)

ftp_nb_fget Lädt eine Datei vom FTP-Server und schreibt sie in eine lokale Datei (nicht-blockierend)

Beschreibung

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

ftp_nb_fget() lädt eine entfernte Datei von einem FTP-Server.

Der Unterschied zwischen dieser Funktion und ftp_fget() ist, dass diese Funktion die Datei asynchron überträgt, so dass Ihr Programm noch andere Operationen ausführen kann während die Datei gespeichert wird.

Parameter-Liste

ftp_stream

Der Verbindungshandler der FTP-Verbindung.

handle

Ein geöffneter Dateizeiger, in dem die Daten gespeichert werden.

remote_file

Der Pfad zur Datei auf dem Server.

mode

Der Transfer-Modus. Muss entweder FTP_ASCII oder FTP_BINARY sein.

resumepos

Rückgabewerte

Gibt FTP_FAILED oder FTP_FINISHED oder FTP_MOREDATA zurück.

Beispiele

Beispiel #1 ftp_nb_fget()-Beispiel

<?php

// Öffne eine Datei zum Lesen
$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);

// Download initialisieren
$ret ftp_nb_fget($conn_id$fp$fileFTP_BINARY);
while (
$ret == FTP_MOREDATA) {

   
// Irgendwas machen
   
echo ".";

   
// Download forsetzen
   
$ret ftp_nb_continue($conn_id);
}
if (
$ret != FTP_FINISHED) {
   echo 
"Es gab einen Fehler bei der Übertragung.";
   exit(
1);
}

// Dateizeiger schließen
fclose($fp);
?>

Siehe auch

  • ftp_nb_get() - Überträgt eine Datei von dem FTP-Server und speichert sie lokal (nicht blockierend)
  • ftp_nb_continue() - Nimmt die Übertragung einer Datei wieder auf (nicht-blockierend)
  • ftp_fget() - Lädt eine Datei vom FTP-Server und speichert sie in eine geöffnete Datei
  • ftp_get() - Lädt eine Datei von einem FTP-Server herunter


Ein BenutzerBeitrag:
- Beiträge aktualisieren...
pilif at pilif dot ch
16.11.2004 14:53
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



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)