搜尋
首頁php教程php手册php 断点续传功能

php 断点续传功能

Jun 13, 2016 am 10:09 AM
php上傳任務幾個功能包包壓縮文件斷點

断点续传指的是在上传时,将上传任务(一个文件或一个压缩包)人为的划分为几个部分,每一个部分采用一个线程进行上传,下面我们来看看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();
     }
}
?>

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境