PHP 8.3.4 Released!

ftp_nb_fget

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

ftp_nb_fget Ruft eine Datei vom FTP-Server ab und schreibt sie in eine lokale Datei (nicht-blockierend)

Beschreibung

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

ftp_nb_fget() ruft eine Datei von einem FTP-Server ab.

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

Parameter-Liste

ftp

Eine FTP\Connection-Instanz.

stream

Der geöffnete Dateizeiger, in dem die Daten gespeichert werden.

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 Datei auf dem Server, an der der Download beginnen soll.

Rückgabewerte

Gibt FTP_FAILED, FTP_FINISHED oder FTP_MOREDATA zurück.

Changelog

Version Beschreibung
8.1.0 Der Parameter ftp erwartet nun eine FTP\Connection-Instanz; vorher wurde eine Ressource erwartet.
7.3.0 Der Parameter mode ist nun optional. Zuvor war er verpflichtend.

Beispiele

Beispiel #1 ftp_nb_fget()-Beispiel

<?php

// Öffne eine Datei zum Lesen
$file = 'index.php';
$fp = fopen($file, 'w');

$ftp = ftp_connect($ftp_server);

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

// Download initiieren
$ret = ftp_nb_fget($ftp, $fp, $file, FTP_BINARY);
while (
$ret == FTP_MOREDATA) {

// Irgendwas machen
echo ".";

// Download forsetzen
$ret = ftp_nb_continue($ftp);
}
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 vom 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

add a note

User Contributed Notes 1 note

up
6
pilif at pilif dot ch
19 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