Recently, there was a PHP memory overflow problem when exporting. The reason was that the temporary file generated during downloading was too large and could not be accommodated in PHP memory. At first I thought of changing the PHP memory limit, but this was just a temporary solution, so I thought of it. Another method is to read the file in batches and download it.
The following is the source code:
Copy the code The code is as follows:
$sourceFile = "1.tmp"; //The name of the temporary file to be downloaded
$outFile = "User Order.xls"; //The name of the file to be downloaded and saved to the client
$file_extension = strtolower( substr(strrchr($sourceFile, "."), 1)); //Get the file extension
//echo $sourceFile;
if (!ereg("[tmp|txt|rar|pdf|doc ]", $file_extension))exit ("Illegal resource download");
//Check whether the file exists
if (!is_file($sourceFile)) {
die("404 File not found!");
}
$len = filesize($sourceFile); //Get the file size
$filename = basename($sourceFile); //Get the file name
$outFile_extension = strtolower(substr(strrchr($outFile, "."), 1)); //Get the file extension
//Indicate the output browser format according to the extension
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 there is $_SERVER['HTTP_RANGE'] parameter
if (isset ($_SERVER['HTTP_RANGE'])) {
/*Range header Range The header field may request one or more subranges of the 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
Indicates the range after 500 bytes: bytes=500-
The first and last 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 the unconditional GET contains the Range request header, the response will be returned with status code 206 (PartialContent) instead of 200 (OK).
*/
// Connect again after the breakpoint. The value of $_SERVER['HTTP_RANGE'] is bytes=4390912-
list ($a, $range) = explode("=", $_SERVER[' HTTP_RANGE']);
//if yes, download missing part
str_replace($range, "-", $range); //What is this sentence for? . . .
$size2 = $size -1; //The total number of bytes in the file
$new_length = $size2 - $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% of the time
} else {
//First connection
$size2 = $size -1;
header("Content-Range: bytes 0-$size2 /$size"); //Content-Range: bytes 0-4988927/4988928
header("Content-Length: " . $size); //Output total length
}
//Open file
$fp = fopen("$sourceFile", "rb");
//Set the pointer position
fseek($fp, $range);
//Unreal output
while (! feof($fp)) {
//Set the maximum execution time of the file
set_time_limit(0);
print (fread($fp, 1024 * 8)); //Output file
flush (); //Output buffer
ob_flush();
}
fclose($fp);
exit ();
http://www.bkjia.com/PHPjc/327461.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327461.htmlTechArticleA PHP memory overflow problem occurred recently when exporting. The reason is that the temporary file generated is read during downloading. It is too large and cannot be accommodated in the PHP memory. When I opened it, I thought of changing the PHP memory...
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