首页 >后端开发 >php教程 >php下载远程文件类(支持断点续传)_php技巧

php下载远程文件类(支持断点续传)_php技巧

WBOY
WBOY原创
2016-05-17 09:34:31991浏览

简易使用方法: 

复制代码 代码如下:

$object = new httpdownload();
$object->set_byfile($file)%N#H#%;//服务器文件名,包括路径
$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 = 0; //部分下载
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_end = -1;
var $totalsizeref = 0;
var $带宽 = 0;
var $speed = 0;
函数initialize() {
全局$HTTP_SERVER_VARS;
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->bandwidth = $bufsize;
$this->sentSize =$bufsize;
}
$size -= $bufsize;
刷新();
if ($speed > 0 && ($this->bandwidth > $speed*$packet*1024))
{
sleep(1);
$数据包;
}
}
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->bandwidth = $size;
$this->sentSize =$size;
}
其他
{
$this->bandwidth = $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);
$数据包;
}
}
} 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(异常$e)
{
error_log("取消下载n".$e, 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
}
返回 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; //您必须使用处理程序
}
}
?>


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn