CakeFest 2024: The Official CakePHP Conference

ftp_nb_fput

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

ftp_nb_fputSalva il contenuto di un file aperto sul server FTP in modalita' non bloccante

Descrizione

ftp_nb_fput(
    resource $ftp_stream,
    string $remote_file,
    resource $handle,
    int $mode,
    int $startpos = ?
): int

La funzione ftp_nb_fput() carica i dati dalla posizione puntata dal puntatore handle fino a quando non raggiunge la fine del file. Il risultato e' salvato in remote_file sul server FTP. La modalita' di trasferimento, mode specificata deve essere FTP_ASCII oppure FTP_BINARY. La differenza tra questa funzione e la funzione ftp_fput() e' che questa funzione trasferisce il file in modo asincrono, cosicche' il programma puo' eseguire altre operazioni mentre il file viene caricato.

Example #1 Esempio di funzione ftp_nb_fput()

<?php

$file
= 'index.php';

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

$conn_id = ftp_connect($ftp_server);

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

// Inizia il trasferimento del file al server
$ret = ftp_nb_fput($conn_id, $file, $fp, FTP_BINARY);
while (
$ret == FTP_MOREDATA) {

// esegue altre operazioni
echo ".";

// continua il trasferimento...
$ret = ftp_nb_continue($conn_id);
}
if (
$ret != FTP_FINISHED) {
echo
"Errore nel trasferimento del file al server...";
exit(
1);
}

fclose($fp);
?>

Restituisce FTP_FAILED, FTP_FINISHED, oppure FTP_MOREDATA.

Vedere anche ftp_nb_put(), ftp_nb_continue(), ftp_put() e ftp_fput().

add a note

User Contributed Notes 2 notes

up
1
jascha at bluestatedigital dot com
19 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
4 years ago
FTP_FAILED = 0
FTP_FINISHED = 1
FTP_MOREDATA = 2
To Top