Home  >  Article  >  Backend Development  >  PHP continues to maintain long connections, uses flush to continuously update the browser UI, and implements download progress bars.

PHP continues to maintain long connections, uses flush to continuously update the browser UI, and implements download progress bars.

WBOY
WBOYOriginal
2016-08-08 09:31:051232browse

How to use PHP+JS to implement the upload progress bar? Most people may have implemented it, but what about downloading? How? The principle is similar, that is, reading and writing in batches, and how many bytes are read each time. However, the disadvantage of this is that the connection is long. Generally, the two solutions commonly used to implement the download progress bar are: one is to use a socket to maintain communication with the client. For communication, keep a long connection, use flush() to continuously update the browser UI, return the size of the downloaded data, and then display the download speed, progress bar, etc.; the second one is to interact with php and flash to display the progress bar.



文件大小
未知长度
已经下载
0
完成进度
0%
setFileSize($filesize);";//在前台显示文件大小 } $newf = fopen ($newfname, "wb"); $downlen=0; if ($newf) { while(!feof($file)) { $data=fread($file, 1024 * 8 );//默认获取8K $downlen+=strlen($data);//累计已经下载的字节数 fwrite($newf, $data, 1024 * 8 ); echo "";//在前台显示已经下载文件大小 ob_flush(); flush(); } } if ($file) { fclose($file); } if ($newf) { fclose($newf); } } ?>

The above introduces how PHP continues to maintain long connections, uses flush to continuously update the browser UI, and implements download progress bars, including aspects of this. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn