PHP to solve the large file download breakpoint continuation download method details

  • 2020-06-07 04:06:02
  • OfStack

Export recently when a php memory problems, the reason is that lies in the download read generated temporary file is too large, PHP memory cannot accommodate, 1 open so think of change PHP memory limit, but this is only a tactic, then thought about another one method is to read the file by several times, and download.
Here is the source code:

<?php 
$sourceFile = "1.tmp"; // The temporary file name to download  
$outFile = " Customer orders .xls"; // Download the filename saved to the client  
$file_extension = strtolower(substr(strrchr($sourceFile, "."), 1)); // Gets the file extension  
//echo $sourceFile; 
if (!ereg("[tmp|txt|rar|pdf|doc]", $file_extension))exit (" Illegal download "); 
// Checks if the file exists  
if (!is_file($sourceFile)) { 
die("<b>404 File not found!</b>"); 
} 
$len = filesize($sourceFile); // Get file size  
$filename = basename($sourceFile); // Get file name  
$outFile_extension = strtolower(substr(strrchr($outFile, "."), 1)); // Gets the file extension  
// By extension   Indicates the output browser format  
switch ($outFile_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 the output browser format  
header("Content-Type: $ctype"); 
header("Content-Disposition: attachment; filename=" . $outFile); 
header("Accept-Ranges: bytes"); 
$size = filesize($sourceFile); 
// If you have $_SERVER['HTTP_RANGE'] parameter  
if (isset ($_SERVER['HTTP_RANGE'])) { 
/*Range Header fields  Range A header field can request an entity's 1 One or more subranges.  
 For example,  
 Said the head 500 Bytes: bytes=0-499 
 According to the first 2 a 500 Byte: bytes=500-999 
 Said the last 500 Bytes: bytes=-500 
 said 500 Range after bytes: bytes=500- 
 The first 1 And the last 1 Bytes: bytes=0-0,-1 
 Specify several ranges at the same time: bytes=500-600,601-999 
 But the server can ignore this request header if it is unconditional GET contains Range Request header, the response will be a status code 206 ( PartialContent ) Return rather than return 200  ( OK ).  
*/ 
//  Connect again after breakpoint  $_SERVER['HTTP_RANGE']  The value of the  bytes=4390912- 
list ($a, $range) = explode("=", $_SERVER['HTTP_RANGE']); 
//if yes, download missing part 
str_replace($range, "-", $range); // What's that for...  
$size2 = $size -1; // Total number of bytes of file  
$new_length = $size2 - $range; // Gets the length of the next download  
header("HTTP/1.1 206 Partial Content"); 
header("Content-Length: $new_length"); // Input the chief  
header("Content-Range: bytes $range$size2/$size"); //Content-Range: bytes 4908618-4988927/4988928 95% when  
} else { 
// The first 1 Time to connect  
$size2 = $size -1; 
header("Content-Range: bytes 0-$size2/$size"); //Content-Range: bytes 0-4988927/4988928 
header("Content-Length: " . $size); // Total output  
} 
// Open the file  
$fp = fopen("$sourceFile", "rb"); 
// Set pointer position  
fseek($fp, $range); 
// Virtual output  
while (!feof($fp)) { 
// Sets the maximum execution time of the file  
set_time_limit(0); 
print (fread($fp, 1024 * 8)); // The output file  
flush(); // The output buffer  
ob_flush(); 
} 
fclose($fp); 
exit (); 


Related articles: