php远程下载文件
<?php
/*
本源碼來源于網(wǎng)絡(luò)
http://user.qzone.qq.com/292672703
*/
header("content-Type: text/html; charset=utf-8"); //定義編碼
set_time_limit (0);//不限時 24 * 60 * 60
//語言包數(shù)組
$lang_cn = array ('0' => '文件地址','1' => '輸入密碼','2' => '下載耗時','3' => '微秒,文件大小','4' => '字節(jié)','5' => '下載成功','6' => '無效密碼','7' => '請重新輸入','8' => '遠程文件下載','9' => '不能打開文件','10'=> '不能寫入文件','11'=> '文件地址','12'=> '下載時間','13'=> '文件不可寫入','14'=> '成功地將','15'=> '操作記錄成功寫入!','16'=> '系統(tǒng)已將此次操作寫入日志記錄!','17'=> '寫入失敗','18'=> '文件不存在,試圖創(chuàng)建,','19'=> '創(chuàng)建失敗!','20'=>'文件大小','21'=>'未知','22'=>'已經(jīng)下載','23'=>'完成進度','24'=>'必須為絕對地址,且前面要加http://'
);
//China,中文
$lang_en = array ('0' => 'File','1' => 'Pass','2' => 'DownTime','3' => 'Ms, file size','4' => 'Byte','5' => 'Download complete','6' => 'Invalid password','7' => 'Please try again','8' => 'Happy flying blog - Remote File Download','9' => 'Can not open file','10'=> 'Can not write file','11'=> 'Query File','12'=> 'Query Time','13'=> 'file not writeable','14'=> 'I have success save','15'=> 'Write successful!','16'=> 'The operating system has written to the log records!','17'=> 'Success or failure','18'=> 'File does not exist, attempting to create,','19'=> 'Create Failed','20'=>'File Size','21'=>'Unknown length','22'=>'Have downloaded','23'=>'Download progress','24'=>'Must be an absolute address'
);
//English,英文
$Language = $lang_cn; //切換語言
$Archives = 'log.txt'; //Log文件
$Folder = 'qq292672703/'; //下載目錄
$password = '292672703'; //管理密碼
?>
<!--簡單控制地址長度-->
<SCRIPT language=javascript>
function CheckPost()
{if (myform.url.value.length<10){alert("文件地址不能小于10個字符,請認真填寫!");myform.url.focus();return false;}
}
</SCRIPT><!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" lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $Language[8]; ?></title>
</head><body>
<form method="post" name="myform" οnsubmit="return CheckPost();">
<?php echo $Language[0]; ?>: <input name="url" type="text" value="http://" size="50%"/> <font color="red"><?php echo $Language[24]; ?></font><br>
<?php echo $Language[1]; ?>: <input name="password" type="password"size="30%" /><br>
<input name="submit" type="submit" value="確認下載" />
</form><br /><table border="1" width="100%"><tr><td width="20%"><?php echo $Language[20]; ?></td><td width="80%"><font color="red"><div id="filesize"><?php echo $Language[21]; ?></font> <?php echo $Language[4]; ?></div></td></tr><tr><td> <?php echo $Language[22]; ?></td><td><font color="red"><div id="downloaded">0</font> <?php echo $Language[4]; ?></div></td></tr><tr><td> <?php echo $Language[23]; ?></td><td><font color="red"><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></font></td></tr>
</table>
<!--文件計算、進度顯示-->
<script type="text/javascript">//文件長度var filesize=0;function $(obj){return document.getElementById(obj);}//設(shè)置文件長度function setFileSize(fsize){filesize=fsize;$("filesize").innerHTML=fsize;}//設(shè)置已經(jīng)下載的,并計算百分比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><?php
//密碼驗證
if ($_POST['password'] == $password)
{class runtime {var $StartTime = 0;var $StopTime = 0;function get_microtime(){list($usec, $sec) = explode(' ', microtime());return ((float)$usec + (float)$sec);}function start() {$this->StartTime = $this->get_microtime();}function stop() {$this->StopTime = $this->get_microtime();}function spent() { return round(($this->StopTime - $this->StartTime) * 1000, 1);}}//消耗時間
$runtime= new runtime;
$runtime->start();// 下載
if (!isset($_POST['submit'])) die();$destination_folder = $Folder;if(!is_dir($destination_folder))mkdir($destination_folder,0777);
$url = $_POST['url'];
$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"];$newfname = $destination_folder . basename($url);//不是所有的文件都會先返回大小的,//有些動態(tài)頁面不先返回總大小,這樣就無法計算進度了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); // 累計已經(jīng)下載的字節(jié)數(shù)fwrite($newf, $data, 1024 * 8 );echo "<script>setDownloaded($downlen);</script>"; //在前臺顯示已經(jīng)下載文件大小ob_flush();flush();}
}
if ($file)
{fclose($file);
}if ($newf)
{fclose($newf);
}$runtime->stop();//停止計算//亂七八糟的東西 -0-;$downtime = '<p>'.$Language[2].':<font color="blue"> '.$runtime->spent().' </font>'.$Language[3].'<font color="blue"> '.$headers["Content-Length"].' </font>'.$Language[4].'.</p><br>';$downok = '<p><font color="red">'.$Language[5].'!'.date("Y-m-d H:i:s").'</font></p><br>';
}
elseif(isset($_POST['password']))
{$passerror = '<p><font color="red">'.$Language[6].'!'.$Language[7].'!</font></p><br>';
}$Export = $downtime.$downok.$passerror;
if(isset($_POST['url']) && ($_POST['password'] == $password))
{$filename = $Archives;$somecontent = $Language[11].': '.$url."\r\n".$Language[2].": ".$runtime->spent().$Language[3].": ".$headers["Content-Length"].$Language[4]."\r\n".$Language[12].': '.date("Y-m-d H:i:s")."\r\n"."\r\n"; if (!file_exists($filename)){$echo_1 = $Language[18];if (!fopen($filename, 'w')){$echo_2 = $Language[19];}}
// 文件操作if (is_writable($filename)) //判斷是否可寫
{if (!$handle = fopen($filename, 'a+')) //打開文件
{$echo_3 = $Language[9].$filename; //當打不開時
} else{if (fwrite($handle, $somecontent) === false)//寫入
{$echo_4 = $Language[10].$filename;} else{$echo_5 = $Language[15];}fclose($handle);//關(guān)閉連接
}
}
else
{$echo_6 = $Language[17];
}
}
$echo = $echo_1.$echo_2.$echo_3.$echo_4.$echo_5.$echo_6;
?><?php echo $Export; ?>
<p><font color="blue"><?php echo $echo; ?></font></p>
</body>
</html>
?
轉(zhuǎn)載于:https://www.cnblogs.com/yhdsir/p/4924086.html
總結(jié)
- 上一篇: VHDL数字秒表的设计
- 下一篇: VHDL汽车尾灯控制器的设计