PHP 下載進度條

來源:互聯網
上載者:User

標籤:rip   執行時間   while   return   load   art   無法   turn   array   

 

<?php    //下載資料    header(‘Content-Type:text/html;charset=utf-8‘);?><html><body><table border="1" width="300">  <tr>    <td width="100">檔案大小</td>    <td width="200"><div id="filesize">未知長度</div></td>  </tr>  <tr>    <td>已經下載</td>    <td><div id="downloaded">0</div></td>  </tr>  <tr>    <td>完成進度</td>    <td><div id="progressbar" style="float:left;width:1px;text-align:center;color:#FFFFFF;background-color:#0066CC"></div>      <div id="progressText" style=" float:left">0%</div></td>  </tr></table><script type="text/JavaScript">//檔案長度var filesize=0;function $(obj) {return document.getElementById(obj);}//設定檔案長度function setFileSize(fsize) {    filesize=fsize;    $("filesize").innerHTML=fsize;}//設定已經下載的,並計算百分比function setDownloaded(fsize) {    $("downloaded").innerHTML=fsize;    if(filesize>0) {        var percent=Math.round(fsize*100/filesize);        $("progressbar").style.width=(percent+"%");        if(percent>0) {            $("progressbar").innerHTML=percent+"%";            $("progressText").innerHTML="";        } else {            $("progressText").innerHTML=percent+"%";        }    }}</script><?PHPob_start();@set_time_limit(300);//設定該頁面最久執行時間為300秒$url="http://dl_dir.qq.com/qqfile/qq/QQ2010/QQ2010Beta3.exe";//要下載的檔案$newfname="QQ2010Beta3.exe";//本地存放位置,也可以是E:\Temp\QQ2010Beta3.exe,這樣做在Win7下要設定相應許可權$file = fopen ($url, "rb");if ($file) {    //擷取檔案大小    $filesize = -1;    $headers = get_headers($url, 1);    if ((!array_key_exists("Content-Length", $headers))) $filesize=0;    $filesize = $headers["Content-Length"];        //不是所有的檔案都會先返回大小的,有些動態網頁面不先返回總大小,這樣就無法計算進度了    if ($filesize != -1) {        echo "<script>setFileSize($filesize);</script>";//在前台顯示檔案大小    }    $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 "<script>setDownloaded($downlen);</script>";//在前台顯示已經下載檔案大小            ob_flush();            flush();        }    }    if ($file) {        fclose($file);    }    if ($newf) {        fclose($newf);    }}?></body></html>

 

PHP 下載進度條

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.