FTP amount FTP-FTP
Is it possible to determine how much data has been transferred using the PHP FTP module ?
Pseudocode
... connect to server
ftp_nb_put(file...)
while(true) {
$data = ftp_nb_continue(file...);
if ($data === FTP_MOREDATA) {
continue ... get amount transfered ...
} else {
break ... check if finished etc ...
}
}
source to share
Not.
Strangely (and unfortunately not) there doesn't seem to be a way to determine how many bytes were loaded by a previous call ftp_nb_continue
with this PHP extension.
As an aside, you have several errors:
-
You should check the result
ftp_nb_put
in the same way as you check the resultftp_nb_continue
, since the transfer starts from the first, not the last, -
Your loop stops when displayed
FTP_MOREDATA
, but it should only stop whenFTP_MOREDATA
not displayed.
... connect to server
$result = ftp_nb_put(file...)
while ($result === FTP_MOREDATA) {
$result = ftp_nb_continue(file...);
}
source to share
You probably got the answer, but for anyone looking ... It's an ftp upload function with a progress callback. $ lcfn = local filename $ rmfn = remote filename
function ftp_upload($conn, $lcfn, $rmfn, $progress)
{
$ret = false;
$_pc = -1;
$totalBytes = filesize($lcfn);
$fp = fopen($lcfn, 'rb');
$state = @ftp_nb_fput($conn, $rmfn, $fp, FTP_BINARY);
if($state !== FTP_FAILED){
while($state === FTP_MOREDATA){
$doneSofar = ftell($fp);
$percent = (integer)(($doneSofar / $totalBytes) * 100);
if($_pc != $percent){
$progress($percent);
$_pc = $percent;
}
$state = @ftp_nb_continue($conn);
}
if($state === FTP_FINISHED){
if($_pc != 100){
$progress(100);
}
$ret = true;
}else{
//error: not finished
}
}else{
//error: failure
}
fclose($fp);
return $ret;
}
source to share