PHP ftp_nb_get get local partial file size while d

2019-07-21 09:35发布

My code:

$conn_id        = ftp_connect($ip_address, $port);
$login_result   = ftp_login($conn_id, $username, $password);

if ((!$conn_id) || (!$login_result)) {
    $p->setProgressBarProgress(0,"Connection error", "red");
}else{
    $size_ftp = ftp_size($conn_id, $file_ftp);

    $ret = ftp_nb_get($conn_id, $file_locally, $file_ftp, FTP_BINARY);
    while ($ret == FTP_MOREDATA) {
        $size_locally = ( file_exists($file_locally) ? filesize($file_locally) : "-");
        $perc = round(($size_locally/$size_ftp)*100);
        $p->setProgressBarProgress($perc, common::formatBytes($size_locally)." of ".common::formatBytes($size_ftp), "orange");
        // Continue downloading...
        $ret = ftp_nb_continue($conn_id);
    }
    if ($ret != FTP_FINISHED) {
        $p->setProgressBarProgress(100,"Download failed", "red");
    }else{
        $p->setProgressBarProgress(100,"Download complete", "green");
    }
}

The line:

$size_locally = ( file_exists($file_locally) ? filesize($file_locally) : "-");

Gets the initial file size, eg: 1460 (which is 1.43 kb), and thats where the problem is.. the file size doesnt change according to whats being downloaded. If I refresh the folder in windows explorer I can see the file size is changing as it downloads more. If I echo the filesize while its looping, I see an endless: 1460 1460 1460 1460 1460 1460

I've tried flusing, ob_flush, etc.. doesnt work.

At the top of my script, I have this line:

ob_end_clean();
ini_set('output_buffering', '0');

Which is necessary to get the progress bar to refresh (which works).

How can I get the latest file size, while its incrementing in size?

标签: php ftp
1条回答
贼婆χ
2楼-- · 2019-07-21 10:35

UPDATE: http://php.net/manual/en/function.filesize.php See comment "if you recently appended something to file, and closed it then this method will not show appended data:"..

Solution: You should insert a call to clearstatcache() before calling filesize()

查看更多
登录 后发表回答