PHP implementation file Download breakpoint continuation

Source: Internet
Author: User
Tags explode php download ranges

<?php

/** PHP Download Breakpoint continued * From:php100*/functionDl_file_resume ($file){      //detects if a file exists    if(!Is_file($file)) { die("<b>404 File not Found!</b>"); }                   $len=filesize($file);//Get File Size    $filename=basename($file);//Get file name    $file _extension=Strtolower(substr(STRRCHR($filename, "."), 1);//get file name extension//Indicate output browser format according to extension    Switch($file _extension ) {          Case"EXE":$ctype= "Application/octet-stream"; Break;  Case"Zip":$ctype= "Application/zip"; Break;  Case"MP3":$ctype= "Audio/mpeg"; Break;  Case"MPG":$ctype= "Video/mpeg"; Break;  Case"Avi":$ctype= "Video/x-msvideo"; Break; default:$ctype= "Application/force-download"; }            //Begin Writing Headers    Header("Cache-control:"); Header("Cache-control:public"); //setting the output browser format    Header("Content-type:$ctype"); if(strstr($_server[' Http_user_agent '], "MSIE")) {//if it is IE browser        #workaround for IE filename bug with multiple periods/multiple dots in filename        #That adds square brackets to Filename-eg. Setup.abc.exe becomes Setup[1].abc.exe        $iefilename=Preg_replace('/\\./', '%2e ',$filename,Substr_count($filename, ‘.‘) -1); Header("Content-disposition:attachment; Filename=\\ "$iefilename\\""); } Else {         Header("Content-disposition:attachment; Filename=\\ "$filename\\""); }     Header("Accept-ranges:bytes"); $size=filesize($file); //If there is a $_server[' http_range ' parameter    if(isset($_server[' Http_range '])) { /*---------------------------Range Header field a range header field can request one or more child ranges of an entity. For example, represents the first 500 bytes: bytes=0-499 represents the second 500 bytes: bytes=500-999 represents the last 500 bytes: bytes=-500 represents the range after 500 bytes: bytes=500-First and last byte: Byte    S=0-0,-1 specifies several ranges: bytes=500-600,601-999 but the server can ignore this request header, and if the unconditional get contains a range request header, the response is returned as a status code of 206 (partialcontent) instead of a (OK). ---------------------------*/      //the value of $_server[' Http_range '] is connected again after the breakpoint bytes=4390912-             List($a,$range)=Explode("=",$_server[' Http_range ']); //If yes, download missing part        Str_replace($range, "-",$range);//What does that say?    $size 2=$size-1;//total number of files bytes        $new _length=$size 2-$range;//get the length of the next download        Header("http/1.1 206 Partial Content"); Header("Content-length:$new _length");//Input Total length        Header("Content-range:bytes$range $size2/$size");//when Content-range:bytes 4908618-4988927/4988928 95%,}Else{//First time Connection        $size 2=$size-1; Header("Content-range:bytes 0-$size 2/$size");//content-range:bytes 0-4988927/4988928        Header("Content-length:".)$size);//Total output Length    }     //Open File    $fp=fopen("$file"," RB "); //Set pointer position    fseek($fp,$range); //Unreal Output     while(!feof($fp)){         //set the maximum file execution time        Set_time_limit(0); Print(fread($fp, 1024*8));//Output File        Flush();//output buffering        Ob_flush(); }     fclose($fp); Exit; } Dl_file_resume ("1.zip");//1.zip file of the sibling directory//---------------------------------------//File download is not supported for the continuation of the breakpoint. //---------------------------------------Downfile ("1.zip"); functionDownfile ($sFilePath) {    if(file_exists($sFilePath)){        $aFilePath=Explode("/",Str_replace("\\\\","/",$sFilePath),$sFilePath); $sFileName=$aFilePath[Count($aFilePath)-1]; $nFileSize=filesize($sFilePath); Header("Content-disposition:attachment; Filename= ".$sFileName); Header("Content-length:".)$nFileSize); Header("Content-type:application/octet-stream"); ReadFile($sFilePath); }    Else   {        Echo("File does not exist!")); } } ?>//This fragment is from http://www.codesnippet.cn/detail/24112012641.html

Source Address: http://www.phpxs.com/code/1009963/

PHP implementation file Download breakpoint continuation

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.