我正在使用此脚本通过php连接1个连接并恢复服务器上的文件。 一切都还可以,但当超过10个用户同时下载时,我们的主机无法响应其他请求。 10个用户的物理内存使用量:739/2000 MB 进入流程:10/70 资源还可以,但我认为这是因为服务器缓存 请帮我 ... 感谢
<?php
usleep(rand(0,1000000));
function aborts()
{
$file=$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$ip=$_SERVER['REMOTE_ADDR'];
mysql_query("DELETE FROM downloading where file='$file' and ip='$ip'");
$vars = array_keys(get_defined_vars());
for ($i = 0; $i < sizeOf($vars); $i++) {
unset($$vars[$i]);
}
unset($vars,$i);
ob_end_clean();
ob_clean();
exit;
}
if(strpos($_SERVER['REQUEST_URI'],'a.php')){
header("HTTP/1.0 404 Not Found");
echo 'File Not Found !';
exit;
}
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Enter Your Account"');
header('HTTP/1.0 401 Unauthorized');
exit;
} else {
function getout($ha){
$hash = md5($ha);
$chars16 = array(
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5,
'6' => 6, '7' => 7, '8' => 8, '9' => 9, 'a' => 10, 'b' => 11,
'c' => 12, 'd' => 13, 'e' => 14, 'f' => 15
);
$base10 = '0';
for ($i = strlen($hash) - 1; $i > 0; $i--) {
$base10 = bcadd($base10, bcmul($chars16[$hash[$i]], bcpow(16, $i)));
}
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,;.:-_+*?!$%&@#~^=/<>[](){}`';
$base = (string)strlen($chars);
$baseX = '';
while (bccomp($base10, $base) === 1 || bccomp($base10, $base) === 0) {
$baseX = substr($chars, bcmod($base10, $base), 1) . $baseX;
$base10 = preg_replace('/\.\d*$/', '', bcdiv($base10, $base));
}
$baseX = substr($chars, $base10, 1) . $baseX;
return $baseX;
}
$u=$_SERVER['PHP_AUTH_USER'];
$p=getout($_SERVER['PHP_AUTH_PW']);
mysql_connect('localhost','x','x');
mysql_select_db('x');
$go = mysql_query("SELECT * FROM accounts where username='$u' and password='$p'");
$number = mysql_num_rows($go);
if($number>0){
$go = mysql_query("SELECT expire FROM accounts where username='$u' and password='$p'");
$data=mysql_fetch_array($go);
$expire=$data[0];
}else{
if(strpos('!'. file_get_contents('../.htpasswd'),$u)){
header('Location: http://'.$_SERVER['SERVER_NAME'] . str_replace('bPzID/','',str_replace('/highspeed/','/',$_SERVER['REQUEST_URI'])));
exit;
};
header('WWW-Authenticate: Basic realm="Enter Your DownloadRooz Account"');
header('HTTP/1.0 401 Unauthorized');
exit;
}
if($expire>time()){
header('Location: http://'.$_SERVER['SERVER_NAME'] . str_replace('/highspeed/','/',$_SERVER['REQUEST_URI']));
exit;
};
header('HTTP/1.0 403 Forbidden');
exit;
$file=$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$ip=$_SERVER['REMOTE_ADDR'];
$go = mysql_query("SELECT * FROM downloading where file='$file' and ip='$ip'");
$number = mysql_num_rows($go);
if($number>0){
exit;
}else{
register_shutdown_function('aborts');
mysql_query("INSERT INTO downloading (file,ip,connections,vip) VALUES ('$file','$ip',1,0)");
$file_path = '.' . str_replace('%20',' ',str_replace('/highspeed/','/',str_replace('%28','(',str_replace('%29',')',$_SERVER['REQUEST_URI']))));
$path_parts = pathinfo($file_path);
$file_name = $path_parts['basename'];
$file_ext = $path_parts['extension'];
// allow a file to be streamed instead of sent as an attachment
$is_attachment = isset($_REQUEST['stream']) ? false : true;
// make sure the file exists
if (is_file($file_path))
{
$file_size = filesize($file_path);
$file = @fopen($file_path,"rb");
if ($file)
{
// set the headers, prevent caching
header("Pragma: public");
header("Expires: -1");
header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");
header("Content-Disposition: attachment; filename=\"$file_name\"");
// set appropriate headers for attachment or streamed file
if ($is_attachment)
header("Content-Disposition: attachment; filename=\"$file_name\"");
else
header('Content-Disposition: inline;');
// set the mime type based on extension, add yours if needed.
$ctype_default = "application/octet-stream";
$content_types = array(
"exe" => "application/octet-stream",
"zip" => "application/zip",
"mp3" => "audio/mpeg",
"mpg" => "video/mpeg",
"avi" => "video/x-msvideo",
);
$ctype = isset($content_types[$file_ext]) ? $content_types[$file_ext] : $ctype_default;
header("Content-Type: " . $ctype);
//check if http_range is sent by browser (or download manager)
if(isset($_SERVER['HTTP_RANGE']))
{
list($size_unit, $range_orig) = explode('=', $_SERVER['HTTP_RANGE'], 2);
if ($size_unit == 'bytes')
{
//multiple ranges could be specified at the same time, but for simplicity only serve the first range
//http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt
list($range, $extra_ranges) = explode(',', $range_orig, 2);
}
else
{
$range = '';
header('HTTP/1.1 416 Requested Range Not Satisfiable');
exit;
}
}
else
{
$range = '';
}
//figure out download piece from range (if set)
list($seek_start, $seek_end) = explode('-', $range, 2);
//set start and end based on range (if set), else set defaults
//also check for invalid ranges.
$seek_end = (empty($seek_end)) ? ($file_size - 1) : min(abs(intval($seek_end)),($file_size - 1));
$seek_start = (empty($seek_start) || $seek_end < abs(intval($seek_start))) ? 0 : max(abs(intval($seek_start)),0);
//Only send partial content header if downloading a piece of the file (IE workaround)
if ($seek_start > 0 || $seek_end < ($file_size - 1))
{
header('HTTP/1.1 206 Partial Content');
header('Content-Range: bytes '.$seek_start.'-'.$seek_end.'/'.$file_size);
header('Content-Length: '.($seek_end - $seek_start + 1));
}
else
header("Content-Length: $file_size");
set_time_limit(19);
header('Accept-Ranges: bytes');
fseek($file, $seek_start);
while(!feof($file))
{
if(connection_aborted==1){
mysql_query("DELETE FROM downloading where file='$file' and ip='$ip'");
$vars = array_keys(get_defined_vars());
for ($i = 0; $i < sizeOf($vars); $i++) {
unset($$vars[$i]);
}
unset($vars,$i);
ob_end_clean();
ob_clean();
exit;
}
usleep(100000);
print(@fread($file,2400));
ob_flush();
flush();
if (connection_status()!=0)
{
@fclose($file);
mysql_query("DELETE FROM downloading where file='$file' and ip='$ip'");
$vars = array_keys(get_defined_vars());
for ($i = 0; $i < sizeOf($vars); $i++) {
unset($$vars[$i]);
}
unset($vars,$i);
ob_end_clean();
ob_clean();
exit;
}
}
// file save was a success
@fclose($file);
aborts();
exit;
}
else
{
// file couldn't be opened
header("HTTP/1.0 500 Internal Server Error");
exit;
}
}
}
}
?>
答案 0 :(得分:1)
这是我在prohackr112.tk上所做的事情:
<?php
$file = "yoursite.com/" . $_GET['file'];
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
将yoursite.com
替换为您的网站名称,这将从网址中的file
下载文件。