当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 52

cmginex / YurunHttp
暂停

forked from 宇润 / YurunHttp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Download.php 3.04 KB
一键复制 编辑 原始数据 按行查看 历史
宇润 提交于 2017-08-17 14:20 . 修复分块下载的一些错误
<?php
namespace Yurun\Until;
class Download
{
use \Yurun\Until\ClassEvent;
/**
* HttpRequest
* @var HttpRequest
*/
public $http;
/**
* 下载地址
* @var string
*/
public $url;
/**
* 参数
* @var string
*/
public $params;
/**
* 请求方法
* @var string
*/
public $method;
/**
* HttpResponse
*
* @var HttpResponse
*/
public $response;
/**
* 断点续传是否开启,默认为自动检测。常量BreakContinue::
* @var int
*/
public $breakContinue = BreakContinue::AUTO;
/**
* 文件大小
* @var int
*/
public $fileSize;
/**
* 是否开启断点续传
* @var bool
*/
public $isBreakContinue;
/**
* 每个分块大小,单位:字节,默认为1M
* @var int
*/
public $blockSize = 1048576;
public function __construct($url, $params = array(), $method = 'GET')
{
$this->url = $url;
$this->params = $params;
$this->method = $method;
$this->http = new HttpRequest();
}
/**
* 获取文件大小,单位:字节。
* @return mixed
*/
public function getFileSize()
{
$this->response = $this->http->headers(array(
'Range' => 'bytes=0-1',
))->options(array(
CURLOPT_NOBODY => true
))->send($this->url, $this->params, $this->method);
if(isset($this->response->headers['Content-Range']))
{
list(, $length) = explode('/', $this->response->headers['Content-Range']);
return (int)$length;
}
else
{
return false;
}
}
public function download($filename)
{
$this->fileSize = $this->getFileSize();
if($this->fileSize)
{
$canBreakContinue = isset($this->response->headers['Content-Range']);
switch($this->breakContinue)
{
case BreakContinue::AUTO:
$this->isBreakContinue = $canBreakContinue;
break;
case BreakContinue::ON:
$this->isBreakContinue = (true === $canBreakContinue);
break;
case BreakContinue::OFF:
$this->isBreakContinue = false;
break;
}
}
else
{
$canBreakContinue = $this->isBreakContinue = false;
}
$this->http->options(array(
CURLOPT_NOBODY => false
));
if($this->isBreakContinue)
{
$fp = fopen($filename, 'a+');
if(false === $fp)
{
throw new \Exception('打开本地文件失败');
}
$begin = filesize($filename);
if(false === $begin)
{
throw new \Exception('获取本地文件大小失败');
}
while($begin < $this->fileSize)
{
$length = min($this->fileSize - $begin, $this->blockSize);
$this->response = $this->http->headers(array(
'Range' => 'bytes=' . $begin . '-' . ($begin + $length),
))->send($this->url, $this->params, $this->method);
if(false === fwrite($fp, $this->response->body))
{
fclose($fp);
throw new \Exception('文件写入失败');
}
$begin += $this->response->headers['Content-Length'];
$this->trigger('progressChanged', array(
'length' => $this->fileSize,
'completeLength' => $begin,
'percent' => $begin / $this->fileSize,
));
}
fclose($fp);
}
else
{
$this->http->download($filename, $this->url, $this->params, $this->method);
}
}
}
PHP
1
https://gitee.com/cmginex/YurunHttp.git
git@gitee.com:cmginex/YurunHttp.git
cmginex
YurunHttp
YurunHttp
master

搜索帮助