Home  >  Article  >  Backend Development  >  Detailed explanation of how to enable PHP to implement breakpoint resume downloading

Detailed explanation of how to enable PHP to implement breakpoint resume downloading

*文
*文Original
2018-01-03 09:15:232177browse

How to make PHP realize breakpoint resume download? In fact, to put it simply, it is to obtain the range of the file requested by the user through this variable $_SERVER['HTTP_RANGE'], and then the program controls the output of the file. For example, the first request for a file ranges from 0 to 999 bytes, the second request is for 1000 to 1999 bytes, and so on. Each time it requests 1000 bytes of content, the program then uses the fseek function to obtain the corresponding file location. Then output. I hope to be helpful.

$fname = './05e58c19552bb26b158f6621a6650899'; 
$fp = fopen($fname,'rb'); 
$fsize = filesize($fname); 
if (isset($_SERVER[&#39;HTTP_RANGE&#39;]) && ($_SERVER[&#39;HTTP_RANGE&#39;] != "") && preg_match("/^bytes=([0-9]+)-$/i", $_SERVER[&#39;HTTP_RANGE&#39;], $match) && ($match[1] < $fsize)) { 
$start = $match[1]; 
} else { 
$start = 0; 
} 
@header("Cache-control: public"); 
@header("Pragma: public"); 
if ($start > 0) { 
fseek($fp, $start); 
Header("HTTP/1.1 206 Partial Content"); 
Header("Content-Length: " . ($fsize - $start)); 
Header("Content-Ranges: bytes" . $start . "-" . ($fsize - 1) . "/" . $fsize); 
} else { 
header("Content-Length: $fsize"); 
Header("Accept-Ranges: bytes"); 
} 
@header("Content-Type: application/octet-stream"); 
@header("Content-Disposition: attachment;filename=1.rm"); 
fpassthru($fp);

You can also take a look at how Discuz!’s attachment.php file implements breakpoint resumption. Please look at the code:

also obtains the range of the file requested by the user through $_SERVER['HTTP_RANGE']. For details, you can view its source code analysis. Here I'm just going to start a discussion.

$range = 0; 
if($readmod == 4) { 
dheader(&#39;Accept-Ranges: bytes&#39;); 
if(!emptyempty($_SERVER[&#39;HTTP_RANGE&#39;])) { 
list($range) = explode(&#39;-&#39;,(str_replace(&#39;bytes=&#39;, &#39;&#39;, $_SERVER[&#39;HTTP_RANGE&#39;]))); 
$rangesize = ($filesize - $range) > 0 ? ($filesize - $range) : 0; 
dheader(&#39;Content-Length: &#39;.$rangesize); 
dheader(&#39;HTTP/1.1 206 Partial Content&#39;); 
dheader(&#39;Content-Range: bytes=&#39;.$range.&#39;-&#39;.($filesize-1).&#39;/&#39;.($filesize)); 
} 
}

Related recommendations:

php The perfect implementation code of the download function

php Detailed explanation of file reading series methods

php file caching function

The above is the detailed content of Detailed explanation of how to enable PHP to implement breakpoint resume downloading. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn