Black Rabbit
Новичок
Download больших файлов- какой протокол выбрать?
Здравствуйте!
Пишу скрипт download-а с поддержкой докачки. Файл отдаю по протоколу HTTP. Не лучше ли будет реализовать это через ftp и если да, то в чем преимущества?
Текущая реализация такова:
<?
set_time_limit (0);
require_once("config.php");
require_once("common.php");
require_once("classes/adodb/adodb.inc.php");
//*******************************************************************//
//** making connection with database ********************************//
$conn = &ADONewConnection($conf['db']['type']);
$conn->Connect($conf['db']['host'], $conf['db']['user'], $conf['db']['pass'], $conf['db']['name']);
$conn->debug = 0;
$conn->setFetchMode(ADODB_FETCH_ASSOC);
//*******************************************************************//
if(isset($_GET['m']))
{
switch($_GET['m'])
{
case("movies"): $table=$conf['tables']['movies']; break;
case("music"): $table=$conf['tables']['music']; break;
case("games"): $table=$conf['tables']['games']; break;
case("soft"): $table=$conf['tables']['soft']; break;
case("clips"): $table=$conf['tables']['clips']; break;
case("skins"): $table=$conf['tables']['skins']; break;
default: header("Location: index.php");exit(); break;
}
}
$id= get_integer($_GET['id']);
if(isAllowed()&&$fileName=getFile())
{
$fd = fopen($fileName,"rb");
$fileSize=filesize($fileName);
if(isset($_SERVER["HTTP_RANGE"]))
{
preg_match ("/bytes=(\d+)-/", $_SERVER["HTTP_RANGE"], $m);
$contentSize = $fileSize - intval($m[1]);
$p1 = $fileSize-$contentSize;
$p2 = $fileSize-1;
$p3 = $fileSize;
fseek ($fd, $p1);
header ("HTTP/1.1 206 Partial Content");
header ("Date: " . getGMTDateTime());
header ("X-Powered-By: PHP/" . phpversion());
header ("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header ("Cache-Control: None");
header ("Pragma: no-cache");
header ("Accept-Ranges: bytes");
header("Content-Type: application/force-download\r\n");
header ("Content-Disposition: attachment; filename=\"".basename($fileName)."\"");
header ("Content-Range: bytes " . $p1 . "-" . $p2 . "/" . $p3);
header ("Content-Length: " . $contentSize);
header ("Content-Type: application/octet-stream");
header ("Proxy-Connection: close");
header ("");
}
else
{
$query="update $table set downloads=downloads+1 where id=".$id;
$conn->_query($query) or die();
$contentSize = $fileSize;
header ("HTTP/1.1 200 OK");
header ("Date: " . getGMTDateTime ());
header ("X-Powered-By: PHP/" . phpversion());
header("Content-Type: application/force-download\r\n");
header ("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header ("Cache-Control: None");
header ("Pragma: no-cache");
header ("Accept-Ranges: bytes");
header ("Content-Disposition: attachment; filename=\"".basename($fileName)."\"");
header ("Content-Length: " . $contentSize);
header ("Age: 0");
header ("Content-Type: application/octet-stream");
header ("Proxy-Connection: close");
header ("");
}
$contents = fread ($fd, $contentSize);
print $contents;
fclose ($fd);
exit();
}
else
header("HTTP/1.1 404 Object Not Found");
function getGMTDateTime ()
{
$offset = date("O");
$roffset = "";
if ($offset[0] == "+")
{
$roffset = "-";
}
else
{
$roffset = "+";
}
$roffset .= $offset[1].$offset[2];
return (date ("D, j M Y H:i:s", strtotime ($roffset . " hours ")) . " GMT");
}
function getFile(){
global $conf,$conn,$table,$id;
$query="select files from {$table} where id=".$id;
$res=$conn->Execute($query) or die("err");
$data=$res->GetRows();
if(count($data)==0)
return false;
if($files=unserialize($data[0]['files']))
{
$file_to_download=$files[0];
if(isset($_GET['file']))
{
for($i=0;$i<count($files);$i++)
{
if($_GET['file']==basename($files[$i]))
{
$file_to_download=$files[$i];
break;
}
}
}
}
else
$file_to_download=$data[0]['files'];
$file_to_download=$conf['path']['downloads'].$_GET['m']."/".$file_to_download;
if(file_exists($file_to_download)&&is_file($file_to_download))
return $file_to_download ;
return false;
}
?>
Здравствуйте!
Пишу скрипт download-а с поддержкой докачки. Файл отдаю по протоколу HTTP. Не лучше ли будет реализовать это через ftp и если да, то в чем преимущества?
Текущая реализация такова:
<?
set_time_limit (0);
require_once("config.php");
require_once("common.php");
require_once("classes/adodb/adodb.inc.php");
//*******************************************************************//
//** making connection with database ********************************//
$conn = &ADONewConnection($conf['db']['type']);
$conn->Connect($conf['db']['host'], $conf['db']['user'], $conf['db']['pass'], $conf['db']['name']);
$conn->debug = 0;
$conn->setFetchMode(ADODB_FETCH_ASSOC);
//*******************************************************************//
if(isset($_GET['m']))
{
switch($_GET['m'])
{
case("movies"): $table=$conf['tables']['movies']; break;
case("music"): $table=$conf['tables']['music']; break;
case("games"): $table=$conf['tables']['games']; break;
case("soft"): $table=$conf['tables']['soft']; break;
case("clips"): $table=$conf['tables']['clips']; break;
case("skins"): $table=$conf['tables']['skins']; break;
default: header("Location: index.php");exit(); break;
}
}
$id= get_integer($_GET['id']);
if(isAllowed()&&$fileName=getFile())
{
$fd = fopen($fileName,"rb");
$fileSize=filesize($fileName);
if(isset($_SERVER["HTTP_RANGE"]))
{
preg_match ("/bytes=(\d+)-/", $_SERVER["HTTP_RANGE"], $m);
$contentSize = $fileSize - intval($m[1]);
$p1 = $fileSize-$contentSize;
$p2 = $fileSize-1;
$p3 = $fileSize;
fseek ($fd, $p1);
header ("HTTP/1.1 206 Partial Content");
header ("Date: " . getGMTDateTime());
header ("X-Powered-By: PHP/" . phpversion());
header ("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header ("Cache-Control: None");
header ("Pragma: no-cache");
header ("Accept-Ranges: bytes");
header("Content-Type: application/force-download\r\n");
header ("Content-Disposition: attachment; filename=\"".basename($fileName)."\"");
header ("Content-Range: bytes " . $p1 . "-" . $p2 . "/" . $p3);
header ("Content-Length: " . $contentSize);
header ("Content-Type: application/octet-stream");
header ("Proxy-Connection: close");
header ("");
}
else
{
$query="update $table set downloads=downloads+1 where id=".$id;
$conn->_query($query) or die();
$contentSize = $fileSize;
header ("HTTP/1.1 200 OK");
header ("Date: " . getGMTDateTime ());
header ("X-Powered-By: PHP/" . phpversion());
header("Content-Type: application/force-download\r\n");
header ("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header ("Cache-Control: None");
header ("Pragma: no-cache");
header ("Accept-Ranges: bytes");
header ("Content-Disposition: attachment; filename=\"".basename($fileName)."\"");
header ("Content-Length: " . $contentSize);
header ("Age: 0");
header ("Content-Type: application/octet-stream");
header ("Proxy-Connection: close");
header ("");
}
$contents = fread ($fd, $contentSize);
print $contents;
fclose ($fd);
exit();
}
else
header("HTTP/1.1 404 Object Not Found");
function getGMTDateTime ()
{
$offset = date("O");
$roffset = "";
if ($offset[0] == "+")
{
$roffset = "-";
}
else
{
$roffset = "+";
}
$roffset .= $offset[1].$offset[2];
return (date ("D, j M Y H:i:s", strtotime ($roffset . " hours ")) . " GMT");
}
function getFile(){
global $conf,$conn,$table,$id;
$query="select files from {$table} where id=".$id;
$res=$conn->Execute($query) or die("err");
$data=$res->GetRows();
if(count($data)==0)
return false;
if($files=unserialize($data[0]['files']))
{
$file_to_download=$files[0];
if(isset($_GET['file']))
{
for($i=0;$i<count($files);$i++)
{
if($_GET['file']==basename($files[$i]))
{
$file_to_download=$files[$i];
break;
}
}
}
}
else
$file_to_download=$data[0]['files'];
$file_to_download=$conf['path']['downloads'].$_GET['m']."/".$file_to_download;
if(file_exists($file_to_download)&&is_file($file_to_download))
return $file_to_download ;
return false;
}
?>