This article is an example of the implementation of PHP download breakpoints to continue the transmission method. Share to everyone for your reference.
The specific implementation code is as follows:
Copy Code code as follows:
<?php
/*
* PHP Download Breakpoint continued transmission
*/
function Dl_file_resume ($file) {
Detect 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
To indicate 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");
Set 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 are $_server[' http_range ' parameters
if (Isset ($_server[' Http_range ')) {
/* ---------------------------
The Range Header field Range header field can request one or more child scopes for an entity. For example, the first 500 bytes are represented: bytes=0-499 represents the Second 500 byte: bytes=500-999 represents the last 500 bytes: bytes=-500 represents 500 bytes later: bytes=500-First and last byte: Byte S=0-0,-1 specifies several scopes at the same time: bytes=500-600,601-999 but the server can ignore this request header, and if the unconditional get contains the range request header, the response is returned in status Code 206 (partialcontent) instead of (OK).
---------------------------*/
The value of the $_server[' Http_range ' after the breakpoint is reconnected again bytes=4390912-
List ($a, $range) =explode ("=", $_server[' Http_range '));
If yes, download missing part
Str_replace ($range, "-", $range);//What's this?
Total number of bytes $size 2= $size -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)) {
To set the maximum execution time for a file
Set_time_limit (0);
Print (Fread ($fp, 1024*8));//Output file
Flush ()//output buffer
Ob_flush ();
}
Fclose ($FP);
Exit
}
Dl_file_resume ("1.zip");//The 1.zip file of the sibling directory
//---------------------------------------
File downloads that do not support the continuation of breakpoints are not supported.
//---------------------------------------
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 will help you with your PHP program design.