Is is possible to detect how much data has been transfered using PHP's FTP module?
Pseudo Code
... 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 ...
}
}
Your probably got an answer by now, but for anyone searching... This is an ftp upload function with 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With