素材牛VIP会员

php大文件断点下载实例教程下载

 所属分类:PHP实例-函数/算法,常用实例教程

 浏览:3689次  评论:14次  更新时间:2020-09-06
牛币素材VIP可免积分下载
php大文件断点下载实例教程下载
积分说明:注册即送10牛币,每日签到可获得5牛币,成为VIP会员可永久免牛币下载!   充值积分   充值会员   更多说明»
素材描述:今天分享下PHP大文件(超过100m以上)下载,PHP如何断点下载大文件

基础属性

  • 难易高级
  • 语言PHP
  • 数据库

详细介绍

PHP

require_once('download.class.php'); 
date_default_timezone_set('Asia/Shanghai'); 
error_reporting(E_STRICT); 
 
function errorHandler($errno, $errstr, $errfile, $errline) { 
    echo '<p>error:', $errstr, '</p>'; 
    exit(); 
} 
 
set_error_handler('errorHandler'); 
define('IS_DEBUG', true); 
 
$filePath = 'test.zip'; 
$mimeType = 'audio/x-matroska'; 
$range = isset($_SERVER['HTTP_RANGE']) ? $_SERVER['HTTP_RANGE'] : null; 
if (IS_DEBUG) { 
//    $range = "bytes=1000-1999\n2000"; 
//    $range = "bytes=1000-1999,2000";  
//    $range = "bytes=1000-1999,-2000";  
//    $range = "bytes=1000-1999,2000-2999";  
} 
set_time_limit(0); 
$transfer = new Transfer($filePath, $mimeType, $range); 
if (IS_DEBUG) { 
    $transfer->setIsLog(true); 
} 
$transfer->send();

download.class.php

/** 
 * 文件传输,支持断点续传。 
 * 2g以上超大文件也有效 
 * @author MoXie 
 */ 
class Transfer { 
 
    /** 
     * 缓冲单元 
     */ 
    const BUFF_SIZE = 5120; // 1024 * 5 
 
    /** 
     * 文件地址 
     * @var <String> 
     */ 
 
    private $filePath; 
 
    /** 
     * 文件大小 
     * @var <String> Php超大数字 字符串形式描述 
     */ 
    private $fileSize; 
 
    /** 
     * 文件类型 
     * @var <String> 
     */ 
    private $mimeType; 
 
    /** 
     * 请求区域(范围) 
     * @var <String> 
     */ 
    private $range; 
 
    /** 
     * 是否写入日志 
     * @var <Boolean> 
     */ 
    private $isLog = false; 
 
    /** 
     * 
     * @param <String> $filePath 文件路径 
     * @param <String> $mimeType  文件类型 
     * @param <String> $range 请求区域(范围) 
     */ 
    function __construct($filePath, $mimeType = null, $range = null) { 
        $this->filePath = $filePath; 
        $this->fileSize = sprintf('%u', filesize($filePath)); 
        $this->mimeType = ($mimeType != null) ? $mimeType : "application/octet-stream"; //  bin 
        $this->range = trim($range); 
    } 
 
    /** 
     *  获取文件区域 
     * @return <Map> {'start':long,'end':long} or null 
     */ 
    private function getRange() { 
        /** 
         *  Range: bytes=-128 
         *  Range: bytes=-128 
         *  Range: bytes=28-175,382-399,510-541,644-744,977-980 
         *  Range: bytes=28-175\n380 
         *  type 1 
         *  RANGE: bytes=1000-9999 
         *  RANGE: bytes=2000-9999 
         *  type 2 
         *  RANGE: bytes=1000-1999 
         *  RANGE: bytes=2000-2999 
         *  RANGE: bytes=3000-3999 
         */ 
        if (!empty($this->range)) { 
            $range = preg_replace('/[\s|,].*/', '', $this->range); 
            $range = explode('-', substr($range, 6)); 
            if (count($range) < 2) { 
                $range[1] = $this->fileSize; // Range: bytes=-100 
            } 
            $range = array_combine(array('start', 'end'), $range); 
            if (empty($range['start'])) { 
                $range['start'] = 0; 
            } 
            if (!isset($range['end']) || empty($range['end'])) { 
                $range['end'] = $this->fileSize; 
            } 
            return $range; 
        } 
        return null; 
    } 
 
    /** 
     * 向客户端发送文件 
     */ 
    public function send() { 
        $fileHande = fopen($this->filePath, 'rb'); 
        if ($fileHande) { 
            // setting 
            ob_end_clean(); // clean cache 
            ob_start(); 
            ini_set('output_buffering', 'Off'); 
            ini_set('zlib.output_compression', 'Off'); 
            $magicQuotes = get_magic_quotes_gpc(); 
//            set_magic_quotes_runtime(0); 
            // init 
            $lastModified = gmdate('D, d M Y H:i:s', filemtime($this->filePath)) . ' GMT'; 
            $etag = sprintf('w/"%s:%s"', md5($lastModified), $this->fileSize); 
            $ranges = $this->getRange(); 
            // headers 
            header(sprintf('Last-Modified: %s', $lastModified)); 
            header(sprintf('ETag: %s', $etag)); 
            header(sprintf('Content-Type: %s', $this->mimeType)); 
            $disposition = 'attachment'; 
            if (strpos($this->mimeType, 'image/') !== FALSE) { 
                $disposition = 'inline'; 
            } 
            header(sprintf('Content-Disposition: %s; filename="%s"', $disposition, basename($this->filePath))); 
 
            if ($ranges != null) { 
                if ($this->isLog) { 
                    $this->log(json_encode($ranges) . ' ' . $_SERVER['HTTP_RANGE']); 
                } 
                header('HTTP/1.1 206 Partial Content'); 
                header('Accept-Ranges: bytes'); 
                header(sprintf('Content-Length: %u', $ranges['end'] - $ranges['start'])); 
                header(sprintf('Content-Range: bytes %s-%s/%s', $ranges['start'], $ranges['end'], $this->fileSize)); 
                // 
                fseek($fileHande, sprintf('%u', $ranges['start'])); 
            } else { 
                header("HTTP/1.1 200 OK"); 
                header(sprintf('Content-Length: %s', $this->fileSize)); 
            } 
            // read file 
            $lastSize = 0; 
            while (!feof($fileHande) && !connection_aborted()) { 
                $lastSize = sprintf("%u", bcsub($this->fileSize, sprintf("%u", ftell($fileHande)))); 
                if (bccomp($lastSize, self::BUFF_SIZE) > 0) { 
                    $lastSize = self::BUFF_SIZE; 
                } 
                echo fread($fileHande, $lastSize); 
                ob_flush(); 
                flush(); 
            } 
            set_magic_quotes_runtime($magicQuotes); 
            ob_end_flush(); 
        } 
        if ($fileHande != null) { 
            fclose($fileHande); 
        } 
    } 
 
    /** 
     * 设置记录 
     * @param <Boolean> $isLog  是否记录 
     */ 
    public function setIsLog($isLog = true) { 
        $this->isLog = $isLog; 
    } 
 
    /** 
     * 记录 
     * @param <String> $msg  记录信息 
     */ 
    private function log($msg) { 
        try { 
            $handle = fopen('transfer_log.txt', 'a'); 
            fwrite($handle, sprintf('%s : %s' . PHP_EOL, date('Y-m-d H:i:s'), $msg)); 
            fclose($handle); 
        } catch (Exception $e) { 
            // null; 
        } 
    } 
 
}

讨论这个素材(14)回答他人问题或分享使用心得会奖励牛币

wa***10  
2020年03月20日

感谢分享,赞一个~

回复
馨***2  
2020年03月21日

感谢分享,赞

回复
wt***00  
2020年04月23日

技术不错,值得学习,对我很有帮助

回复
冷***知  
2020年05月13日

先评论后下载,支持

回复
那***哦  
2020年06月08日

下载多图片可以用么

回复
闪***星  
2020年06月15日

断点下载,这个厉害,可以学习下

回复
ba***pc  
2020年07月12日

断点下载 不错 深度研究下 希望有帮助

回复
青***e  
2020年07月19日

很好的功能啊 断点下载 下载视频的时候经常用到啊

回复
hx***lf  
2020年08月10日

断点不错的东西,值得学习

回复
ki***xx  
2020年08月12日

下载的这个也很有用

回复
wx***44  
2021年03月18日

谢谢分享,一直理解不透,今天才懂

回复
qq***17  
2021年04月16日

谢谢分享,挺不错的教程

回复
qq***18  
2022年11月24日

收藏下,每天学习下

回复
wx***36  
2023年01月20日

感谢!正好用到!!

回复
 文明上网,理性发言!   😉 阿里云幸运券,戳我领取
我的牛币余额:0
所需牛币:100 开始下载

牛币获取:签到、评论、充值    » 在线充值(10牛币=1元)