在翻墙的虚拟主机上建立一个简单的下载功能

Posted on Posted in php

有一个可以访问 out 的虚拟主机,做个中转下载的功能

<?php

header("Content-type: text/html; charset=utf-8");
function getFile($url,$path) {
$error='';

$fp = fopen ($path, 'w+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
if (isCertInfoAvailable()){
curl_setopt($ch, CURLOPT_CERTINFO, TRUE);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
$data=curl_exec($ch);
$curlError=curl_error($ch);
curl_close($ch);
fclose($fp);

if($data==false){
$error.='fail download.<br />'.$curlError;
}
return($error.$curlError);

}
function isCertInfoAvailable() {
$curlDetails = curl_version();
return version_compare($curlDetails['version'], '7.19.1') != -1;
}
//getFile("http://openssh-lpk.googlecode.com/files/openssh-lpk_openldap.schema","openssh-lpk_openldap.schema");

?>
<?php if($_SERVER['REQUEST_METHOD'] == "GET"):?>
<html>
<head><title>墙外下载</title></head>
<body>
<form action="" method="post" autocomplete="on">
<table>
<tr><td>username: <input type="text" name="username"></td></tr>
<tr><td>password: <input type="password" name="password"></td></tr>
<tr><td>fileurl : <input type="url" name="fileurl"></td></tr>
<tr><td>filename: <input type="text" name="filename"></td></tr>
<tr><td><input type="submit" value="Download"></td></tr>
</table>
</form>
fileurl :文件的url <br />
filename:保存的文件名(当前路径)
</body>
</html>
<?php else:?>
<?php
$nowdir = dirname(__FILE__);
$username = $_POST['username'];
$password = $_POST['password'];
if($username=="username" and $password=="password"){ // 用户名密码
$downurl = $_POST['fileurl'];
$downfile = $_POST['filename'];
getFile($downurl,$downfile);
Header('Location: http://'.$_SERVER['HTTP_HOST'].'/'.$downfile);
exit;
}else{
echo "用户名或密码错误";
sleep(1);
header('Cache-control: private, must-revalidate');
}
?>
<? endif;?>

Leave a Reply

Your email address will not be published. Required fields are marked *

19 − 13 =