I have made similar script
Feature:
- Switching mirror every download request (simple bandwidth balancer)
- Checking if file exist on mirror and if not, checking another mirror. If file cant be found on any mirror sends to client "404 Not Found".
PHP Code:
<?
//Written by KawMAN
ini_set('display_errors', '1');
//set_time_limit ("10"); //Optional timeout
$mirrors = array();
//Mirrors here
$mirrors[] = "http://mirror1.wsciekle.pl/cstrike";
$mirrors[] = "http://mirror2.wsciekle.pl/fd";
$mirrors[] = "http://mirror3.wsciekle.pl/cstrike";
$mircount = count($mirrors);
$fn=$_GET["fn"]; //Filename
// Get first mirror to check
$clnr = GetNextMir();
if($clnr == -1) exit("cant read from fd_redir_c.txt");
//Change 'first mirror' to next mirror on list
if($clnr>=$mircount-1) {
SetNextMir(0);
} else {
SetNextMir($clnr+1);
}
$loop=0;
while(!redir($clnr)) {
if($clnr>=$mircount-1) {
$clnr=0;
} else {
$clnr++;
}
if($loop>$mircount) { //When all mirrors return Not Found, script return Not Found
header("HTTP/1.0 404 Not Found");
exit('Not Found');
}
$loop++;
}
function GetNextMir() {
$uchwyt=fopen("fd_redir_c.txt", "r");
if ($uchwyt === false) {
return -1;
}
$fdc = fread($uchwyt, filesize("fd_redir_c.txt"));
fclose($uchwyt);
return (int)$fdc;
}
function SetNextMir($clnr) {
$uchwyt=fopen("fd_redir_c.txt", "w");
fwrite($uchwyt, $clnr);
fclose($uchwyt);
}
function redir($nr)
{
global $fn, $mirrors;
$uchwyt = @fopen($mirrors[$nr].$fn,"r");
if ($uchwyt === false)
{
return false;
}
else
{
header("location: ".$mirrors[$nr].$fn);
return true;
}
}
?>
in folder with that script must be file called fd_redir_c.txt with chmod 777
now sv_downloadurl should looks like this:
sv_downloadurl "http://main.wsciekle.pl/fd_redir.php?fn="
also you can create folder for example "fastdownload" and put to it file .htaccess with something like this:
PHP Code:
RewriteEngine On
RewriteRule ^(.*) http://main.wsciekle.pl/fd_redir.php?fn=/$1 [R,NC]
and now sv_downloadurl can looks like :
sv_downloadurl "http://main.wsciekle.pl/fastdownload"
Working example:
http://fastdownload.wsciekle.pl/maps...ood_rm.bsp.bz2 download, check source, download, check source
__________________