本帖最后由 yabuy 于 2019-10-11 23:06 编辑
谷歌搜索找了个自己改下代码,大家需要就拿去吧
- <?php
- if (isset($_POST['url']) and !empty($_POST['url'])) {
- set_time_limit (0);
- function microtime_float() {
- list($usec, $sec) = explode(" ", microtime());
- return ((float)$usec + (float)$sec);
- }
- function RealSize($size) {
- if ($size < 1024) return $size . ' Byte';
- if ($size < 1048576) return round($size / 1024, 2) . ' KB';
- if ($size < 1073741824) return round($size / 1048576, 2) . ' MB';
- if ($size < 1099511627776) return round($size / 1073741824, 2) . ' GB';
- }
- function wrjs() {
- global $current, $total, $size, $time, $inittime, $temptime, $initsize;
- $k = "";
- $per = round(($current / $total) * 100);
- $temptime2 = microtime_float();
- if ($per == 100 || ($temptime2 - $temptime > 1)) {
- $speed = $current / ($temptime2 - $inittime);
- $formatespeed = RealSize($speed) . '/s';
- $formatetotal = RealSize($total);
- $formatecurrent = RealSize($current);
- $pasttime = '已用时:' . round($temptime2 - $inittime) . '秒';
- $lasttime = ' 估计还要:' . round(($total - $current) / $speed) . '秒';
- $str = $per . '|' .$formatespeed.'|'. $pasttime . $lasttime . '|' . $formatecurrent . '|' . $formatetotal;
- @file_put_contents('downinfo.txt', $str);
- $temptime = $temptime2;
- $initsize = $current;
- }
- }
- $destination_folder = './';
- $url = $_POST['url'];
- $info = get_headers($url, 1);
- $total = $info['Content-Length'];
- $current = 0;
- $newfname = $destination_folder . basename($url);
- $file = fopen ($url, "rb");
- $inittime = $temptime = microtime_float();
- $initsize = 0;
- if ($file) {
- $newf = fopen ($newfname, "wb");
- if ($newf)
- while (!feof($file)) {
- $str = fread($file, 1024 * 8);
- $current += strlen($str);
- fwrite($newf, $str, 1024 * 8);
- wrjs();
- }
- echo '<center>下载完毕,共计用时:' . round(microtime_float() - $inittime) . '秒</center>';
- }
- if ($file) {
- fclose($file);
- }
- if ($newf) {
- fclose($newf);
- }
- exit;
- }
- if (isset($_GET['getinfo'])) {
- $arr = file_get_contents('downinfo.txt');
- if ($arr) {
- header("Content-type: text/html; charset=utf-8");
- echo $arr;
- }
- exit;
- }
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>远程下载</title>
- <style>
- *{font-size:12px;}
- .bar1 {background: url("/images/progress_bar_null.gif"); position:relative; text-align:left; height:13px; width:540px; border:1px solid #505050;margin:0 auto;}
- .bar2 {background: url("/images/progress_bar.gif"); position:relative; text-align:left; height:13px; width:0%;}
- .info{width:540px;margin:20px auto;font-size:12px;}
- </style>
- <script type="text/javascript">
- <!--
- var isstart=false;
- function check() {
- if (g('downurl').value=="") {
- alert('为空不能提交');
- return false;
- }
- setTimeout("startdown()",2000);
- return true;
- }
- function frameload() {
- if (isstart) {
- downfinish();
- }
- }
- function downfinish() {
- isstart=false;
- clearInterval(timename);
- }
- function startdown() {
- g('downinfo').style.display="block";
- isstart=true;
- timename=setInterval("getinfo();",500);
- }
- function resetinfo(p,s,t,c,tl) {
- g('down_status').style.width=p+"%";
- g('speed').innerHTML='平均速率:'+s;
- g('time').innerHTML='时间信息:'+t;
- g('current').innerHTML='已下载:'+c;
- g('total').innerHTML='文件大小:'+tl;
- if (p==100) {
- clearInterval(timename);
- }
- }
- function getinfo() {
- if (isstart) {
- ajax({
- url: "<?php echo basename(__FILE__);?>?getinfo="+Math.random(),
- data: "is=ajax" ,
- success: function (responseText) {
- arr=responseText.split('|');
- if (arr.length==5) {
- resetinfo(arr[0],arr[1],arr[2],arr[3],arr[4]);
- } else {
- alert(responseText);
- }
- }
- });
- }
- else {
- clearInterval(timename);
- }
- }
- function ajax(s) {
- var a = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
- with(a) {
- open("POST", s.url, true);
- setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
- send(s.data);
- onreadystatechange = function () {
- readyState == 4 && status == 200 ? s.success(responseText) : null
- }
- }
- };
- function g(ob) {
- return document.getElementById(ob);
- }
- //-->
- </script>
- </head>
- <body>
- <center><form method="post" id="urlform" target="downframe" onsubmit="return check();">
- <div style="display:none;"><iframe name="downframe" src="about:blank"></iframe></div>
- <input name="url" id='downurl' size="50" />
- <input name="submit" value="提交下载地址" type="submit" />
- </form></center>
- <div id="downinfo" style="margin-top:30px;display:none;">
- <div class="bar1" id="upload_status_wrap" align="center">
- <div class="bar2" id="down_status"></div>
- </div>
- <div class="info">
- <div id="speed"> </div>
- <div id="time"> </div>
- <div id="current"> </div>
- <div id="total"> </div>
- </div>
- </div>
- <script language="javascript">
- </script>
- </body>
- </html>
复制代码 |