code: <?php/* @desc: Php download file shows progress @param files to download filename @param name stored to local file 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); }}}
Test:downloadprogress(‘2018.rar‘,‘aaa/01.rar‘);
Output (under CLI):
PHP Download file display progress (for CLI mode or long connection)