本文介绍了下载时,PHP ftp_nb_get获取本地部分文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

  $ conn_id = ftp_connect($ ip_address,$ port); 
$ login_result = ftp_login($ conn_id,$ username,$ password); ($!$ 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-> setmonitorBarProgress($ perc,common :: formatBytes($ size_locally)。of.common :: formatBytes($ size_ftp),orange);
//继续下载...
$ ret = ftp_nb_continue($ conn_id);

如果($ ret!= FTP_FINISHED){
$ p-> setProgressBarProgress(100,下载失败,红色);
} else {
$ p-> setProgressBarProgress(100,下载完成,绿色);


$ / code $ / pre>

行:

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

获取初始文件大小,例如:1460(这是1.43 kb),这就是问题的出处是..文件大小不会根据什么被下载。如果我在Windows资源管理器中刷新文件夹,我可以看到文件大小在下载更多时发生变化。如果我在循环播放时回显文件大小,我会看到无尽的:1460 1460 1460 1460 1460 1460 我已经试过flusing,ob_flush等等。不起作用。 / p>

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

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

获取进度条以进行刷新是非常必要的。



如何获得最新的文件大小,而其大小是递增的?

解决方案

更新:
请参阅注释如果您最近在文件中添加了某些内容,并关闭了它,则此方法不会显示附加数据:..


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?

解决方案

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:"..

这篇关于下载时,PHP ftp_nb_get获取本地部分文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 08:44