ftp_nb_fput

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

ftp_nb_fput — Salva sul server FTP il contenuto di un file aperto (non bloccante)

Descrizione

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

ftp_nb_fput() carica i dati da un puntatore al file in un file remoto sul server FTP.

La differenza tra questa funzione e ftp_fput() è che questa funzione carica il file in modo asincrono, così il programma può eseguire altre operazioni mentre il file viene caricato.

Elenco dei parametri

ftp

Un'istanza di FTP\Connection.

remote_filename

Il percorso del file remoto.

stream

Un puntatore al file aperto sul file locale. La lettura si interrompe alla fine del file.

mode

La modalità di trasferimento. Deve essere FTP_ASCII oppure FTP_BINARY.

offset

La posizione nel file remoto da cui iniziare il caricamento.

Valori restituiti

Restituisce FTP_FAILED oppure FTP_FINISHED oppure FTP_MOREDATA.

Errori/Eccezioni

Throws a ValueError if mode is not FTP_ASCII or FTP_BINARY.

Log delle modifiche

Versione Descrizione
8.1.0 Il parametro ftp ora si aspetta un'istanza di FTP\Connection; in precedenza, era attesa una resource.
8.0.0 Now throws a ValueError if mode is not FTP_ASCII or FTP_BINARY. Previously, an E_WARNING was raised and false was returned.
7.3.0 Il parametro mode ora è opzionale. In precedenza era obbligatorio.

Esempi

Example #1 Esempio di ftp_nb_fput()

<?php

$file = 'index.php';

$fp = fopen($file, 'r');

$ftp = ftp_connect($ftp_server);

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

// Avvia il caricamento
$ret = ftp_nb_fput($ftp, $file, $fp, FTP_BINARY);
while ($ret == FTP_MOREDATA) {

   // Esegue qualsiasi altra operazione
   echo ".";

   // Continua il caricamento...
   $ret = ftp_nb_continue($ftp);
}
if ($ret != FTP_FINISHED) {
   echo "Si è verificato un errore durante il caricamento del file...";
   exit(1);
}

fclose($fp);
?>

Vedere anche:

User Contributed Notes 2 notes

up
2
jascha at bluestatedigital dot com
21 years ago
There is an easy way to check progress while uploading a file.  Just use the ftell function to watch the position in the file handle.  ftp_nb_fput will increment the position as the file is transferred.

Example:

<?

    $fh = fopen ($file_name, "r");
    $ret = ftp_nb_fput ($ftp, $file_name, $fh, FTP_BINARY);
    while ($ret == FTP_MOREDATA) {
        print ftell ($fh)."\n";
        $ret = ftp_nb_continue($ftp);
    }
    if ($ret != FTP_FINISHED) {
        print ("error uploading\n");
        exit(1);
    }
    fclose($fh);

?>

This will print out the number of bytes transferred thus far, every time the loop runs.  Coverting this into a percentage is simply a matter of dividing the number of bytes transferred by the total size of the file.
up
-3
marcopardo at gmx dot de
6 years ago
FTP_FAILED = 0
FTP_FINISHED = 1
FTP_MOREDATA = 2
Adding new notes is disabled. Please see the links in the Found A Problem? section.
To Top