ftp_nb_get

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

ftp_nb_get Überträgt eine Datei vom FTP-Server und speichert sie lokal (nicht blockierend)

Beschreibung

ftp_nb_get ( resource $ftp , string $local_filename , string $remote_filename , int $mode = FTP_BINARY , int $offset = 0 ) : int

ftp_nb_get() ruft eine Datei vom FTP-Server ab und speichert sie in einer lokalen Datei.

Der Unterscheid zwischen dieser Funktion und ftp_get() ist, dass diese Funktion die Datei asynchron überträgt, sodass Ihr Programm noch andere Operationen ausführen kann, während die Datei heruntergeladen wird.

Parameter-Liste

ftp

Die Verbindungskennung der FTP-Verbindung.

local_filename

Der Pfad zur lokalen Datei (wird überschrieben falls die Datei schon existiert).

remote_filename

Der Pfad zur Datei auf dem Server.

mode

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

offset

Die Position in der entfernten Datei, an der der Download beginnen soll.

Rückgabewerte

Gibt FTP_FAILED, FTP_FINISHED oder FTP_MOREDATA zurück.

Changelog

Version Beschreibung
7.3.0 Der Parameter mode ist nun optional. Zuvor war er verpflichtend.

Beispiele

Beispiel #1 ftp_nb_get()-Beispiel

<?php

// Download iniitieren
$ret ftp_nb_get($my_connection"test""README"FTP_BINARY);
while (
$ret == FTP_MOREDATA) {

   
// Irgendwas machen
   
echo ".";

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

Beispiel #2 Einen Download mit ftp_nb_get() fortsetzen

<?php

// Iniitieren
$ret ftp_nb_get($my_connection"test""README"FTP_BINARY,
                      
filesize("test"));
// ODER: $ret = ftp_nb_get($my_connection, "test", "README", FTP_BINARY,
// FTP_AUTORESUME);
while ($ret == FTP_MOREDATA) {

   
// Irgendwas machen
   
echo ".";

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

Beispiel #3 Einen Download mit Hilfe von ftp_nb_get() an Position 100 fortsetzen und in eine neue Datei schreiben

<?php

// Autoseek deaktivieren
ftp_set_option($my_connectionFTP_AUTOSEEKfalse);

// Initialisieren
$ret ftp_nb_get($my_connection"newfile""README"FTP_BINARY100);
while (
$ret == FTP_MOREDATA) {

   
/* ... */

   // Download fortsetzen
   
$ret ftp_nb_continue($my_connection);
}
?>

In dem obigen Beispiel ist newfile 100 Bytes kleiner als README auf dem FTP-Server, weil wir erst beim Offset 100 angefangen haben zu lesen. Hätten wir FTP_AUTOSEEK nicht deaktiviert, so bestünden die ersten 100 Bytes von newfile aus '\0'.

Siehe auch

  • ftp_nb_fget() - Lädt eine Datei vom FTP-Server und schreibt sie in eine lokale Datei (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

add a note add a note

User Contributed Notes 1 note

up
0
passerbyxp at gmail dot com
12 years ago
Note that you may have to keep calling ftp_nb_continue in order to complete the download. For example, if you do this:

<?php
ftp_nb_get
($conn,$localfile,$remotefile,FTP_BINARY)
//do some LONG time work
while(ftp_nb_continue($conn)!=FTP_FINISHED){}
?>

Your local file may only contains a few kilobytes and the later ftp_nb_continue will keep raising warning of no more data (due to connection time out, I guess).

So you may want to do this instead:

<?php
$dl
=ftp_nb_get($conn,$localfile,$remotefile,FTP_BINARY)
//part of long time work
if(ftp_nb_continue($conn)==FTP_MOREDATA) {}
//part of long time work
if(ftp_nb_continue($conn)==FTP_MOREDATA) {}
//continue to do this until you finish the long time work
while(ftp_nb_continue($conn)==FTP_MOREDATA){}
?>

This happened on my Windows XP + PHP 5.3.8 under CLI. Hope this helps someone.
To Top