Reputation: 783
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 ...
}
}
Upvotes: 3
Views: 657
Reputation: 330
For anyone want to show a the upload progress while doing file transfers, this is a great library php-ftp-client to start :
The code
$interval = 1;
$ftp->asyncDownload('illustrations/assets.zip', 'assets.zip', function ($stat) use ($interval) {
ob_end_clean();
ob_start();
echo sprintf(
"speed : %s KB/%ss | percentage : %s%% | transferred : %s KB | second now : %s <br>",
$stat['speed'],
$interval,
$stat['percentage'],
$stat['transferred'],
$stat['seconds']
);
ob_flush();
flush();
}, true, $interval);
Result in the browser :
Upvotes: 0
Reputation: 146
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;
}
Upvotes: 3
Reputation: 385154
No.
Strangely (and quite unfortunately), there does not appear to be any way to determine how many bytes were uploaded by the previous call to ftp_nb_continue
, with this PHP extension.
As an aside, you have a few errors:
You should check the result of ftp_nb_put
in the same way that you check for the result of ftp_nb_continue
, since the transfer begins with the former, not the latter;
Your loop stops when FTP_MOREDATA
is seen, but it should stop only when FTP_MOREDATA
is not seen.
... connect to server
$result = ftp_nb_put(file...)
while ($result === FTP_MOREDATA) {
$result = ftp_nb_continue(file...);
}
Upvotes: 2