簡易使用方法:
複製程式碼 程式碼如下:
$object = new httpdownload();
$object = new httpdownload();
$object = new httpdownload();
$object->filename = $filename;//下載另存為的檔案名稱$object->download( ); 3.原始檔:
複製程式碼 程式碼如下:
class httpdownload {
var $data = null;
var $data_len = 0;
var $data_mod = 0;
var $data_type = 0;
var $data_section = 00 ; //部分下載
var $sentSize=0;
var $handler = array('auth' => null);
var $use_resume = true;
var $use_autoexit = false;
var $use_auth = false;
var $filename = null;
var $mime = null;
var $bufsize = 2048;
var $seek_start = 0;
var $seek_endend = -1;
var $totalsizeref = 0;
var $頻寬= 0;
var $speed = 0;
函數initialize() {
全域$HTTP_SERVER_VARS
if
if; ($this->use_auth) //使用認證
{
if (!$this->_auth()) //不認證
{
header('WWW -驗證:基本領域= 「請輸入您的使用者名稱和密碼」');
header('HTTP/1.0 401 未經授權');
header('狀態:401 未經授權');
if ($this ->use_autoexit) exit();
回傳錯誤;
}
}
if ($this->mime == null) $this->mime = "application/octet-stream"; //預設mime
if (isset($_SERVER['HTTP_RANGE']) || isset($HTTP_SERVER_VARS['HTTP_RANGE']))
{
if (isset($HTTP_SERVER_VARS['HTTP_RANGE'] ) )) $seek_range = substr($HTTP_SERVER_VARS['HTTP_RANGE'] , strlen('bytes='));
else $seek_range = substr($_SERVER['HTTP_RANGE'] , strlen('bytes=')) ;
$range=explode('-',$seek_range);
if ($range[0] > 0)
{
$this->seek_start = intval($range[0] );
}
if ($range[1] > 0) $this->seek_end = intval($range[1]);
else $this->seek_end = -1;
if (!$this->use_resume)
{
$this->seek_start = 0;
//header("HTTP/1.0 404 錯誤請求");
//header("狀態:400 錯誤請求");
//退出;
//返回假;
}
其他
{
$this->data_section = 1;
}
}
其他
{
$this->seek_start = 0;
$this->seek_end = -1;
}
$this->sentSize=0 傳回真;
}
函數頭($size,$seek_start=null,$seek_end=null) {
header('內容型別: ' . $this->mime);
header('Content-Disposition:attachment; filename="' . $this->filename . '"');
header('最後修改時間:' . date('D, d M Y H:i:s GMT' , $this->data_mod));
if ($this->data_section && $this->use_resume)
{
header("HTTP/1.0 206 部分內容");
header( "狀態:206 部分內容");
header('接受範圍:位元組');
header("內容範圍:位元組$seek_start-$seek_end/$size");
header( "內容長度:" .($seek_end - $seek_start + 1));
}
else
{
header("內容長度:$size");
}
}
函數download_ex($size)
{
if (!$this->initialize()) return false;
ignore_user_abort(true);
//這裡用seek end
if ($this->seek_start > ($size - 1)) $this->seek_start = 0;
if ($this->seek_end seek_end = $size - 1;
$this->header($size,$seek,$this->seek_end);
$this->data_mod = time();
回傳真;
}
函數download( ) {
if (!$this->initialize()) return false;
嘗試
{
error_log("開始下載", 3,"/usr/local/www/apache22/LOGS /apache22_php.err");
$seek = $this->seek_start;
$speed = $this->speed;
$bufsize = $this->bufsize;
$封包= 1;
//做一些清理
@ob_end_clean();
$old_status =ignore_user_abort(true);
@set_time_limit(0);
$this->頻寬= 0;
$size = $this->data_len;
if ($this->data_type == 0) //從檔案下載
{
$size = filesize($this->data);
if ($seek > ($size - 1)) $seek = 0;
if ($this->filename == null) $this->filename = basename($this->data);
$res = fopen($this->data,'rb');
if ($seek) fseek($res , $seek);
if ($this->seek_end seek_end = $size - 1;
$this->header($size,$seek,$this->seek_end); //總是使用最後一次查找
$size = $this->seek_end - $seek + 1;
while (!(connection_aborted() || connection_status() == 1) && $size > 0)
{
if ($size { echo fread($res,$size);
$this->頻寬+=$size;
$this->sentSize+=$size;
}
else
{
echo fread($res , $bufsize);
$this->頻寬+=$bufsize;
$this->sentSize+=$bufsize;
}
$size -= $bubuize ;
刷新();
if ($speed > 0 && ($this->bandwidth > $speed*$packet*1024))
{
sleep(1);
$ packet++;
}
}
fclose($res); }
elseif ($this->data_type == 1) //從字串下載
{
if ($seek > ($size - 1)) $seek = 0;
if ($this->seek_end seek_end = $this->data_len - 1;
$this->data = substr($this->data , $seek , $this->seek_end - $seek + 1);
if ($this->filename == null) $this->filename = time();
$size = strlen($this->data);
$this->header($this->data_len,$seek,$this->seek_end);
while (!connection_aborted() && $size > 0) {
if ($size {
$this->頻寬 += $size;
$this->sentSize+=$size;
}
else
{
$this->頻寬 += $bufsize;
$this->sentSize+=$bufsize;
}
echo substr($this->data , 0 , $bufsize);
$this->data = substr($this->data , $bufsize);
$size -= $bufsize;
刷新();
if ($speed > 0 && ($this->bandwidth > $speed*$packet*1024))
{
sleep(1);
$packet++;
}
}
} else if ($this->data_type == 2) {
//只傳送重定向標頭
header('location: ' . $this->
}
if($this->totalsizeref==$this->sentSize )error_log("結束下載", 3,"/usr/local/www/apache22/LOGS/apache22_php.err ") ;
else error_log("下載取消", 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
if ($this->use_autoexit) exit();
/ /恢復舊狀態
ignore_user_abort($old_status);
set_time_limit(ini_get("max_execution_time"));
}
catch(or$e)
{
}
catch(or$e)
{
{ }
回傳true;
}
function set_byfile ($dir) {
if (is_read($dir) && is_file($dir)) {
$this->data_len = 0;
$this->data = $dir; this->data_type = 0;
$this->data_mod = filemtime($dir);
$this->totalsizeref 檔案大小($dir);
傳回真;
} 否則回傳false;
}
function set_bydata($data) {
if ($data == '') return false;
$this->data = $data
$this-> data_len = strlen($data);
$this->data_type = 1;
$this->data_mod = time();
返回真;
}
function set_byurl($data ) {
$this->data = $data;
$this->data_len = 0;
$this->data_type = 2;
回傳真;
}
function set_lastmodtime($time) {
$time = intval($time);
if ($time $this->data_mod = $time;
}
function _auth() {
if (!isset($_SERVER['PHP_AUTH_USER'])) return false;
if (isset($this->handler['auth']) && function_exists($this->handler['auth']))
{
return $this->handler[ 'auth' ]('auth' , $_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW']);
}
否則必須回傳true; //你使用處理程序
}
}
以上就介紹了php下載遠端文件類支援斷點續傳,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。