PHP Breakpoint Continuation function _php Tutorial

Source: Internet
Author: User
Tags ranges
The continuation of the breakpoint refers to the upload task (a file or a compressed package) is divided into several parts, each part of a thread to upload, the following we look at the PHP breakpoint continuation of the implementation method of the function.

The continuation of the breakpoint refers to the upload task (a file or a compressed package) is divided into several parts, each part of a thread to upload, the following we look at the PHP breakpoint continuation of the implementation method of the function.

/**
* Author Widows water
* Support for the continuation of the breakpoint download
* Example code:
* $down = new Sd_download ();
* $down->down (' E:/iso/ms. Office2003SP1.CHS.iso ');
**/
Class Sd_download {

/**
* Start point of download
*
* @access Private
* @var Integer
*/
Private $mDownStart;

/**
* File Size
*
* @access Private
* @var Integer
*/
Private $mFileSize;

/**
* File handle
*
* @access Private
* @var Integer
*/
Private $mFileHandle;

/**
* File Full path
*
* @access Private
* @var String
*/
Private $mFilePath;

/**
* File name to be displayed when downloading files
*
* @access Private
* @var String
*/
Private $mFileName;

/**
* Constructor function
*
* @access Public
* @return void
**/
Public Function __construct () {
}

/**
* Download
*
* @param string $pFilePath file full path
* @param string pfilename file name displayed when downloading, default to 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 of 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 section in the full path
*
* @access Private
* @return String
**/
Private Function GetFileName () {
Return basename ($this->mfilepath);
}

/**
* Send data
*
* @access Private
* @return void
**/
Private Function Send () {
Fpassthru ($this->mfilehandle);
}

/**
* Send 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 www.bkjia.com true http://www.bkjia.com/PHPjc/630477.html techarticle Breakpoint continuation refers to the upload task (a file or a compressed package) is divided into several parts, each part of a thread to upload, below we come ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.