PHP 다운로드 원격 파일 클래스는 중단점 이력서 다운로드를 지원합니다.

WBOY
풀어 주다: 2016-07-29 08:39:11
원래의
1133명이 탐색했습니다.

사용하기 쉬움:

코드 복사 코드는 다음과 같습니다.


$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 $bandwidth = 0;
var $speed = 0;
함수 초기화() {
전역 $HTTP_SERVER_VARS;
if ($this->use_auth) //인증 사용
{
if (!$this->_auth()) //인증 없음
{
header('WWW -인증: 기본 영역="사용자 이름과 비밀번호를 입력하세요"');
header('HTTP/1.0 401 권한 없음');
header('상태: 401 권한 없음');
if ($this->use_autoexit) 종료();
거짓을 반환합니다.
}
}
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='));
그렇지 않으면 $seek_range = substr($_SERVER['HTTP_RANGE'] , strlen('bytes='));
$range = 폭발('-',$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 잘못된 요청");
//종료;
//거짓을 반환합니다.
}
else
{
$this->data_section = 1;
}
}
else
{
$this->seek_start = 0;
$this->seek_end = -1;
}
$this->sentSize=0;
참을 반환합니다.
}
function header($size,$seek_start=null,$seek_end=null) {
header('Content-type: ' . $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('Accept-Ranges: 바이트');
header("콘텐츠 범위: 바이트 $seek_start-$seek_end/$size");
header("콘텐츠 길이: " . ($seek_end - $seek_start 1));
}
else
{
header("콘텐츠 길이: $size");
}
}
function download_ex($size)
{
if (!$this->initialize()) return false;
ignore_user_abort(true);
//여기서 탐색 끝 사용
if ($this->seek_start > ($size - 1)) $this->seek_start = 0;
if ($this->seek_end <= 0) $this->seek_end = $size - 1;
$this->header($size,$seek,$this->seek_end);
$this->data_mod = 시간();
참을 반환합니다.
}
function download() {
if (!$this->initialize()) return false;
해 보세요
{
error_log("begin downloadn", 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
$seek = $this->seek_start;
$speed = $this->속도;
$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) $this->seek_end = $size - 1;
$this->header($size,$seek,$this->seek_end); //항상 마지막 탐색을 사용합니다
$size = $this->seek_end - $seek 1;
while (!(connection_aborted() || 연결_상태() == 1) && $size > 0)
{
if ($size < $bufsize)
{
echo fread($res, $size);
$this->대역폭 = $size;
$this->sentSize =$size;
}
else
{
echo fread($res , $bufsize);
$this->대역폭 = $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) $this->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 < $bufsize)
{
$this->bandwidth = $size;
$this->sentSize =$size;
}
else
{
$this->bandwidth = $bufsize;
$this->sentSize =$bufsize;
}
echo substr($this->data , 0 , $bufsize);
$this->data = substr($this->data , $bufsize);
$크기 -= $buf크기;
플러시();
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("end downloadn", 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) 종료();
//이전 상태 복원
ignore_user_abort($old_status);
set_time_limit(ini_get("max_execution_time"));
}
catch(예외 $e)
{
error_log("cancel downloadn".$e, 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
}
true를 반환합니다.
}
function set_byfile($dir) {
if (is_readable($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 = 시간();
참을 반환합니다.
}
function set_byurl($data) {
$this->data = $data;
$this->data_len = 0;
$this->data_type = 2;
참을 반환합니다.
}
function set_lastmodtime($time) {
$time = intval($time);
if ($time <= 0) $time = 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教程有兴趣的朋友所帮助。

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!