dragon201401 2015-12-19 06:18
浏览 672

如何使用php从服务器下载文件时显示进度条

<?php
$zip_file="test.zip";
$source="abc.com/$zip_file";
$data=file_get_contents("$source");
sleep(1);
//display the progress
$dest=file_put_contents($zip_file,$data);
?>

<div id='status'>progress ...</div>

I want to show the progress bar for every 1 second of downloaded % and remaining % to download.

  • 写回答

1条回答 默认 最新

  • doucitao2944 2015-12-19 06:39
    关注

    Your PHP is going to run server-side. In fact, depending on your server implementation, your PHP is probably going to finish running before your server is even willing to start writing a response based on your PHP's output. You cannot let the client view download progress purely through PHP or any server side code. You will have to implement a progress bar client side, by monitoring the progress of the the download.

    Think of it like this: you want to monitor the client's receipt of data, but the PHP is running on your server. Your server has no idea how much data the client has received.

    If the download you're talking about is literally the download of an entire new web page, per window.location = http://example.com/uri/for/your/php.php or a link or an HTML form, then you will not have any opportunity to display progress to your client. That will be totally up to the browser.

    However, if the download takes place through an XMLHttpRequest (and most manual requests do, even jQuery I think) then you have some hope. You can learn about monitoring the progress of XMLHttpRequest downloads from this answered question.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题