KodExplorer/lib/class/downloader.class.php

162 lines
4.8 KiB
PHP
Raw Normal View History

2016-12-21 08:01:06 +00:00
<?php
2017-03-06 11:28:24 +00:00
/*
* @link http://www.kalcaddle.com/
* @author warlee | e-mail:kalcaddle@qq.com
* @copyright warlee 2014.(Shanghai)Co.,Ltd
* @license http://kalcaddle.com/tools/licenses/license.txt
*/
2016-12-21 08:01:06 +00:00
class downloader {
2017-04-07 13:11:01 +00:00
static function start($url,$save_file,$timeout = 10) {
2017-03-06 11:28:24 +00:00
$data_file = $save_file . '.download.cfg';
$save_temp = $save_file . '.downloading';
2017-04-07 13:11:01 +00:00
//header:{'url','length','name','support_range'}
if(is_array($url)){
$file_header = $url;
}else{
$file_header = url_header($url);
2017-03-06 11:28:24 +00:00
}
2016-12-21 08:01:06 +00:00
2017-04-13 12:24:15 +00:00
$url = $file_header['url'];
2017-04-07 13:11:01 +00:00
//默认下载方式if not support range
if(!$file_header['support_range'] ||
$file_header['length'] == 0 ){
@unlink($save_temp);
@unlink($save_file);
$result = self::file_download_fopen($url,$save_file,$file_header['length']);
if($result['code']) {
return $result;
}else{
@unlink($save_temp);
@unlink($save_file);
2017-04-21 11:23:04 +00:00
return self::file_download_curl($url,$save_file,false,0,$file_header['length']);
2017-04-07 13:11:01 +00:00
}
2017-03-06 11:28:24 +00:00
}
2016-12-21 08:01:06 +00:00
2017-03-06 11:28:24 +00:00
$exists_length = is_file($save_temp) ? filesize($save_temp) : 0;
$content_length = intval($file_header['length']);
if( file_exists($save_temp) &&
time() - filemtime($save_temp) < 3) {//has Changed in 3s,is downloading
return array('code'=>false,'data'=>'downloading');
}
$exists_data = array();
if(is_file($data_file)){
$temp_data = file_get_contents($data_file);
$exists_data = json_decode($temp_data, 1);
}
// exist and is the same file;
if( file_exists($save_file) && $content_length == filesize($save_file)){
@unlink($save_temp);
@unlink($data_file);
return array('code'=>true,'data'=>'exist');
}
2016-12-21 08:01:06 +00:00
2017-03-06 11:28:24 +00:00
// check file is expire
if ($exists_data['length'] != $content_length) {
$exists_data = array('length' => $content_length);
}
if($exists_length > $content_length){
@unlink($save_temp);
}
// write exists data
file_put_contents($data_file, json_encode($exists_data));
2017-04-21 11:23:04 +00:00
$result = self::file_download_curl($url,$save_file,true,$exists_length,$content_length);
2017-04-07 13:11:01 +00:00
if($result['code']){
@unlink($data_file);
2017-03-06 11:28:24 +00:00
}
2017-04-07 13:11:01 +00:00
return $result;
2017-03-06 11:28:24 +00:00
}
2016-12-21 08:01:06 +00:00
2017-03-06 11:28:24 +00:00
// fopen then download
2017-04-07 13:11:01 +00:00
static function file_download_fopen($url, $file_name,$header_size=0){
2017-03-06 11:28:24 +00:00
$file_temp = $file_name.'.downloading';
2017-04-07 13:11:01 +00:00
@set_time_limit(0);
@unlink($file_temp);
if ($fp = @fopen ($url, "rb")){
2017-03-06 11:28:24 +00:00
if(!$download_fp = @fopen($file_temp, "wb")){
return array('code'=>false,'data'=>'open_downloading_error');
}
while(!feof($fp)){
if(!file_exists($file_temp)){//删除目标文件;则终止下载
fclose($download_fp);
return array('code'=>false,'data'=>'stoped');
}
//对于部分fp不结束的通过文件大小判断
clearstatcache();
if( $header_size>0 &&
$header_size==get_filesize(iconv_system($file_temp))
){
break;
}
fwrite($download_fp, fread($fp, 1024 * 8 ), 1024 * 8);
}
//下载完成,重命名临时文件到目标文件
fclose($download_fp);
fclose($fp);
2017-04-21 11:23:04 +00:00
$filesize = get_filesize(iconv_system($file_temp));
if($header_size != 0 && $filesize != $header_size){
return array('code'=>false,'data'=>'file size error');
}
2017-04-07 13:11:01 +00:00
if(!@rename($file_temp,$file_name)){
2017-04-13 12:24:15 +00:00
usleep(round(rand(0,1000)*50));//0.01~10ms
2017-04-07 13:11:01 +00:00
@unlink($file_name);
$res = @rename($file_temp,$file_name);
if(!$res){
2017-04-13 12:24:15 +00:00
return array('code'=>false,'data'=>'rename error![open]');
2017-04-07 13:11:01 +00:00
}
2017-03-06 11:28:24 +00:00
}
return array('code'=>true,'data'=>'success');
}else{
return array('code'=>false,'data'=>'url_open_error');
}
}
2016-12-21 08:01:06 +00:00
2017-04-07 13:11:01 +00:00
// curl 方式下载
// 断点续传 http://www.linuxidc.com/Linux/2014-10/107508.htm
2017-04-21 11:23:04 +00:00
static function file_download_curl($url, $file_name,$support_range=false,$exists_length=0,$length=0){
2017-04-07 13:11:01 +00:00
$file_temp = $file_name.'.downloading';
@set_time_limit(0);
if ($fp = @fopen ($file_temp, "a")){
$ch = curl_init($url);
//断点续传
if($support_range){
curl_setopt($ch, CURLOPT_RANGE, $exists_length."-");
2017-03-06 11:28:24 +00:00
}
2017-04-07 13:11:01 +00:00
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_REFERER,get_url_link($url));
$res = curl_exec($ch);
curl_close($ch);
2017-04-13 12:24:15 +00:00
fclose($fp);
2017-04-21 11:23:04 +00:00
$filesize = get_filesize(iconv_system($file_temp));
if($filesize < $length && $length!=0){
return array('code'=>false,'data'=>'downloading');
}
if($filesize > $length && $length!=0){
//远程下载大小不匹配;则返回正在下载中,客户端重新触发下载
return array('code'=>false,'data'=>'file size error');
}
2017-04-07 13:11:01 +00:00
if($res && filesize($file_temp) != 0){
if(!@rename($file_temp,$file_name)){
@unlink($file_name);
$res = @rename($file_temp,$file_name);
if(!$res){
2017-04-13 12:24:15 +00:00
return array('code'=>false,'data'=>'rename error![curl]');
2017-04-07 13:11:01 +00:00
}
}
return array('code'=>true,'data'=>'success');
2017-03-06 11:28:24 +00:00
}
2017-04-07 13:11:01 +00:00
return array('code'=>false,'data'=>'curl exec error!');
}else{
return array('code'=>false,'data'=>'file create error');
2017-03-06 11:28:24 +00:00
}
}
2016-12-21 08:01:06 +00:00
}