- 代码:
<?php
/*
@desc:php下载文件显示进度
@param file 待下载文件名
@param name 存储到本地文件名
*/
function downloadprogress($file,$name){
$dir = dirname($name);
if(!is_dir($dir)){
mkdir($dir,0755,true);
}
ob_start();
set_time_limit(0);
$fr = fopen ($file, "rb");
if ($fr) {
$content = file_get_contents($file);
$filesize = strlen($content);
$fw = fopen ($name, "wb");
$downlen=0;
$last = 0;
$diff = 0;
echo "[";
if ($fw) {
while(!feof($fr)) {
$data = fread($fr, 1024 * 8 );
$downlen += strlen($data);
fwrite($fw, $data, 1024 * 8 );
$percent = round($downlen/$filesize*100,2);
$diff += $percent - $last;
if($diff > 1){
echo "#";
$diff = 0;
}
$last = $percent;
ob_flush();
flush();
}
echo "#]done";
}
if ($fr) {
fclose($fr);
}
if ($fw) {
fclose($fw);
}
}
}
- 测试:
downloadprogress('2018.rar','aaa/01.rar');
- 输出(cli下):
|