PHP breakpoint resume function_PHP tutorial

WBOY
Release: 2016-07-13 17:07:06
Original
1247 people have browsed it

Breakpoint resumption refers to artificially dividing the upload task (a file or a compressed package) into several parts during upload, and each part is uploaded using a thread. Let's take a look at PHP breakpoints How to implement the resume function.

Breakpoint resumption refers to artificially dividing the upload task (a file or a compressed package) into several parts during upload, and each part is uploaded using a thread. Let's take a look at PHP breakpoints How to implement the resume function.

/**
* Author Yu Enshui
* Supports resume download
* * Example code:
* * $down = new SD_DownLoad();
* * $down->Down('E:/iso/MS.Office2003SP1.CHS.iso');
**/
class SD_DownLoad {
    
     /**
* Download starting point
           *                                   * @access private
* @var integer
                     */
     private $mDownStart;
    
     /**
*File size
           *                                   * @access private
* @var integer
                     */
     private $mFileSize;
    
     /**
             * File handle
           *                                  * @access private
* @var integer
                     */
     private $mFileHandle;
    
     /**
* Full file path
           *                                  * @access private
* @var string
                     */
     private $mFilePath;
    
     /**
* The file name displayed when downloading the file
           *                                   * @access private
* @var string
                     */
     private $mFileName;
    
     /**
* Constructor
           *                                   * @access public
* @return void
**/
     public function __construct() {
     }
    
     /**
* Download
           *                                  * @param string $pFilePath The full path of the file
* @param string pFileName The file name displayed when downloading the file. The default is the actual file name
* @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();
     }

    
     /**
* Initialization file information
           *                                   * @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;
     }
    
     /**
*Set download start point
           *                                   * @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;
          }
     }
    
     /**
* Set http header
           *                                   * @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);
     }
    
     /**
* Get the file name part of the full path
           *                                  * @access private
* @return string
**/
     private function GetFileName() {
          return basename ($this->mFilePath);
     }
    
     /**
         * 发送数据
           *                                  * @access private
* @return void
         **/
Private function Send() {
                fpassthru($this->mFileHandle);
}
​  
/**
*Sending error
           *                                   * @access public
* @return void
**/
Public function SendError() {
​​​​​​@header("HTTP/1.0 404 Not Found");
             @header("Status: 404 Not Found");
exit();
}
}
?>

http://www.bkjia.com/PHPjc/630477.html

truehttp: //www.bkjia.com/PHPjc/630477.htmlTechArticleResumable upload refers to artificially uploading the task (a file or a compressed package) when uploading Divided into several parts, each part is uploaded using a thread. Let's...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!