【问题标题】:PHP ftp_nb_get get local partial file size while downloadingPHP ftp_nb_get 下载时获取本地部分文件大小
【发布时间】:2015-03-10 12:45:50
【问题描述】:

我的代码:

$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");
    }
}

行:

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

获取初始文件大小,例如:1460(即 1.43 kb),这就是问题所在......文件大小不会根据下载的内容而改变。如果我在 Windows 资源管理器中刷新文件夹,我可以看到文件大小随着下载量的增加而变化。如果我在循环时回显文件大小,我会看到无穷无尽:1460 1460 1460 1460 1460 1460

我尝试过flusing、ob_flush等。不起作用。

在我的脚本顶部,我有这行:

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

刷新进度条所必需的(有效)。

我怎样才能获得最新的文件大小,同时它的大小会增加?

【问题讨论】:

    标签: php ftp


    【解决方案1】:

    更新: http://php.net/manual/en/function.filesize.php 见评论“如果你最近在文件中附加了一些东西,然后关闭了它,那么这个方法将不会显示附加的数据:”..

    解决方案:您应该在调用之前插入对 clearstatcache() 的调用 文件大小()

    【讨论】:

      猜你喜欢
      • 2012-06-15
      • 2013-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      相关资源
      最近更新 更多