A file download class that supports breakpoint renewal in PHP

Source: Internet
Author: User
Tags empty file size header php download range ranges sprintf

PHP support breakpoint Continuation, mainly rely on the HTTP protocol header Http_range implementation.

The principle of HTTP breakpoint continuous transmission

HTTP header Range, Content-range ()

Range and Content-range entity headers are used when a general breakpoint in the HTTP header is downloaded.

Range user Request header, specify the position of the first byte and the position of the last byte, such as (range:200-300)

Content-range for response headers

Request to download the entire file:

Get/test.rar http/1.1

Connection:close

host:116.1.219.219

range:bytes=0-801//General request download entire file is bytes=0-or not this head

General Normal response

http/1.1 OK

content-length:801

Content-type:application/octet-stream

Content-range:bytes 0-800/801//801: Total File size

FileDownload.class.php

<?php/** PHP Download class, support breakpoint continuation * date:2013-06-30 * author:fdipzone * ver:1.0 * Func: * Download: Download file * setspeed: Set download speed * GetRange: Get header in range */class filedownload{//Class start Privat   e $_speed = 512;  
    Download speed/** Download * @param string $file The file path to download * @param String $name file name, blank, same as the downloaded file name * @param boolean $reload whether to open a breakpoint continuation/public function download ($file, $name = ', $reload =false) {if (File_exists ($file))  
            {if ($name = = ') {$name = basename ($file);  
            $fp = fopen ($file, ' RB ');  
            $file _size = filesize ($file);  
      
            $ranges = $this->getrange ($file _size);  
            Header (' Cache-control:public ');  
            Header (' Content-type:application/octet-stream ');  
      
            Header (' Content-disposition:attachment filename= '. $name); if ($reload && $ranGes!=null) {//Use the continued header (' http/1.1 206 Partial Content ');  
                      
                Header (' accept-ranges:bytes ');  
                      
                Remaining Length header (sprintf (' content-length:%u ', $ranges [' End ']-$ranges [' Start '])); Range Information Header (sprintf (' Content-range:bytes%s-%s/%s ', $ranges [' Start '], $ranges [' End  
                      
                '], $file _size));  
            FP pointer jumps to the breakpoint position fseek ($fp, sprintf ('%u ', $ranges [' Start ']));  
                }else{header (' http/1.1 OK ');  
            Header (' Content-length: '. $file _size);  
                while (!feof ($fp)) {echo fread ($fp, round ($this->_speed*1024,0));  
                Ob_flush (); Sleep (1);  
      
        For testing, slowing download speed} ($fp!=null) && fclose ($FP);  
        }else{return "; }  
    /** Set Download speed * @param int $speed */Public Function setspeed ($speed) {i  
        F (Is_numeric ($speed) && $speed >16 && $speed <4096) {$this->_speed = $speed; /** Get Header Range Info * View this column more highlights: Http://www.bianceng.cnhttp://www.bianceng.cn/webkf/P  
        hp/* @param int $file _size File size * @return Array/Private Function GetRange ($file _size) { if (Isset ($_server[' Http_range ')) &&!empty ($_server[' Http_range '])) {$range = $_server[' Http_range ']  
            ;  
            $range = preg_replace ('/[\s|,].*/', ', ', $range);  
            $range = Explode ('-', substr ($range, 6));  
            if (count ($range) <2) {$range [1] = $file _size;  
            $range = array_combine (Array (' Start ', ' End '), $range);  
            if (Empty ($range [' start]]) {$range [' start '] = 0;  
       }     if (Empty ($range [' end])} {$range [' end '] = $file _size;  
        return $range;  
    return null; }//Class end?>

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.