CakeFest 2024: The Official CakePHP Conference

ftp_nb_get

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

ftp_nb_getRecupera un file dal server FTP e lo scrive su un file locale in modalita' non bloccante

Descrizione

ftp_nb_get(
    resource $ftp_stream,
    string $local_file,
    string $remote_file,
    int $mode,
    int $resumepos = ?
): int

La funzione ftp_nb_get() recupera remote_file dal server FTP, e lo salva localmente su local_file. La modalita' di trasferimento, mode, specificata deve essere FTP_ASCII oppure FTP_BINARY. La differenza tra questa funzione e la funzione ftp_get() e' che questa funzione recupera il file in modo asincrono, cosicche' il programma puo' eseguire altre operazioni mentre il file viene scaricato.

Restituisce FTP_FAILED, FTP_FINISHED, oppure FTP_MOREDATA.

Example #1 Esempio di funzione ftp_nb_get()

<?php

// Inizia lo scaricamento
$ret = ftp_nb_get($my_connection, "test", "README", FTP_BINARY);
while (
$ret == FTP_MOREDATA) {

// esegue altre operazioni
echo ".";

// Continua lo scaricamento...
$ret = ftp_nb_continue($my_connection);
}
if (
$ret != FTP_FINISHED) {
echo
"Errore nello scaricamento del file...";
exit(
1);
}
?>

Example #2 Ripresa di uno scaricamento con ftp_nb_get()

<?php

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

// esegue altre operazioni
echo ".";

// Continua lo scaricamento...
$ret = ftp_nb_continue($my_connection);
}
if (
$ret != FTP_FINISHED) {
echo
"Errore nello scaricamento del file...";
exit(
1);
}
?>

Example #3 Ripresa di uno scaricamento dalla posizione 100 su un nuovo file con ftp_nb_get()

<?php

// Esclusione di Autoseek (ricerca automatica)
ftp_set_option($my_connection, FTP_AUTOSEEK, false);

// Inizio
$ret = ftp_nb_get($my_connection, "newfile", "README", FTP_BINARY, 100);
while (
$ret == FTP_MOREDATA) {

/* ... */

// Continua lo scaricamento...
$ret = ftp_nb_continue($my_connection);
}
?>

Nell'esempio precedente, "newfile" e' 100 bytes piu' piccolo di "README" sul server FTP perche' la lettura e' iniziata dall'offset 100. Se FTP_AUTOSEEKnon fosse stata disabilitata, i primi 100 bytes di "newfile" sarebbero stati '\0'.

Vedere anche ftp_nb_fget(), ftp_nb_continue(), ftp_get(), e ftp_fget().

add a note

User Contributed Notes 1 note

up
-1
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