search
Homephp教程php手册php 断点续传功能

php 断点续传功能

Jun 13, 2016 am 10:09 AM
phpuploadpeopleTaskseveralFunctionBagcompressionWilldocumentbreakpointhour

断点续传指的是在上传时,将上传任务(一个文件或一个压缩包)人为的划分为几个部分,每一个部分采用一个线程进行上传,下面我们来看看php 断点续传功能的实现方法吧。

断点续传指的是在上传时,将上传任务(一个文件或一个压缩包)人为的划分为几个部分,每一个部分采用一个线程进行上传,下面我们来看看php 断点续传功能的实现方法吧。

/**
    * 作者 于恩水
    * 支持断点续传下载
    * 实例代码:
    *           $down = new SD_DownLoad();
    *          $down->Down('E:/iso/MS.Office2003SP1.CHS.iso');
    **/
class SD_DownLoad {
    
     /**
         * 下载的开始点
         *
         * @access private
         * @var integer
         */
     private $mDownStart;
    
     /**
         * 文件大小
         *
         * @access private
         * @var integer
         */
     private $mFileSize;
    
     /**
         * 文件句柄
         *
         * @access private
         * @var integer
         */
     private $mFileHandle;
    
     /**
         * 文件全路径
         *
         * @access private
         * @var string
         */
     private $mFilePath;
    
     /**
         * 文件下载时显示的文件名
         *
         * @access private
         * @var string
         */
     private $mFileName;
    
     /**
         * 构造函数
         *
         * @access public
         * @return void
         **/
     public function __construct() {
     }
    
     /**
         * 下载
         *
         * @param string $pFilePath 文件全路径
         * @param string pFileName 文件下载时显示的文件名,缺省为实际文件名
         * @access public
         * @return void
         **/
     public function Down($pFilePath, $pFileName = '') {
          $this->mFilePath = $pFilePath;
          if(!$this->IniFile()) $this->SendError();
          $this->mFileName = empty($pFileName) ? $this->GetFileName() : $pFileName;
         
          $this->IniFile();
          $this->SetStart();
          $this->SetHeader();
         
          $this->Send();
     }

    
     /**
         * 初始化文件信息
         *
         * @access private
         * @return boolean
         **/
     private function IniFile() {
          if(!is_file($this->mFilePath)) return false;
          $this->mFileHandle = fopen($this->mFilePath, 'rb');
          $this->mFileSize = filesize($this->mFilePath);
          return true;
     }
    
     /**
         * 设置下载开始点
         *
         * @access private
         * @return void
         **/
     private function SetStart() {
          if (!empty($_SERVER['HTTP_RANGE']) && preg_match("/^bytes=([d]?)-([d]?)$/i", $_SERVER['HTTP_RANGE'], $match)) {
               if(empty($match[1])) $this->mDownStart = $match[1];
               fseek($this->mFileHandle, $this->mDownStart);
          }
          else {
               $this->mDownStart = 0;
          }
     }
    
     /**
         * 设置http头
         *
         * @access private
         * @return void
         **/
     private function SetHeader() {
          @header("Cache-control: public");
          @header("Pragma: public");
          Header("Content-Length: " . ($this->mFileSize - $this->mDownStart));
          if ($this->mDownStart > 0) {
               @Header("HTTP/1.1 206 Partial Content");
               Header("Content-Ranges: bytes" . $this->mDownStart . "-" . ($this->mFileSize - 1) . "/" . $this->mFileSize);
          }
          else {
               Header("Accept-Ranges: bytes");
          }
          @header("Content-Type: application/octet-stream");
          @header("Content-Disposition: attachment;filename=" . $this->mFileName);
     }
    
     /**
         * 获取全路径里的文件名部分
         *
         * @access private
         * @return string
         **/
     private function GetFileName() {
          return basename ($this->mFilePath);
     }
    
     /**
         * 发送数据
         *
         * @access private
         * @return void
         **/
     private function Send() {
          fpassthru($this->mFileHandle);
     }
    
     /**
         * 发送错误
         *
         * @access public
         * @return void
         **/
     public function SendError() {
          @header("HTTP/1.0 404 Not Found");
          @header("Status: 404 Not Found");
          exit();
     }
}
?>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor