PHP implementation of the download breakpoint continuation of the method, PHP implementation of the breakpoint _php tutorial

Source: Internet
Author: User
Tags php download php programming ranges

PHP implementation of the download breakpoint continuation of the method, PHP implementation of the breakpoint to continue to pass


In this paper, we describe the method for PHP to implement download breakpoint continuation. Share to everyone for your reference.

The specific implementation code is as follows:
Copy CodeThe code is as follows: <?php
/*
* PHP Download Breakpoint Continuation
*/
function Dl_file_resume ($file) {

Detects if a file exists
if (!is_file ($file)) {Die ("404 File Not found!"); }

$len = FileSize ($file);//Get File size
$filename = basename ($file);//Get file name
$file _extension = Strtolower (substr (STRRCHR ($filename, "."), 1));//Get File name extension

Indicates the output browser format according to the 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's 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 ')) {
/* ---------------------------
The Range Header field 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's the sentence for?
$size 2= $size Total bytes of -1;//files
$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")//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);//Output total 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 downloads that do not support the continuation of a breakpoint.

//---------------------------------------

Downfile ("1.zip");

function Downfile ($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!");
}
}
?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/910607.html www.bkjia.com true http://www.bkjia.com/PHPjc/910607.html techarticle PHP Implementation of the download breakpoint continuation of the method, PHP implementation of the breakpoint continuation of this article describes the PHP implementation of the download breakpoint continuation of the method. Share to everyone for your reference. The specific implementation code is as follows: ...

  • Related Article

    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.