php download files, such as txt files.
The effect is that the download box that comes with the browser pops up and the save as operation appears. Sometimes memory overflow and timeout may occur.
If it times out, set set_time_limit(0);
If memory overflow occurs, it may be because the amount of data taken out from the database is too large.
If you are reading from a file and there is a memory overflow, it means that the code reading method is incorrect. You need to call files or filegetcontens.
If it is fopen, give a buffer with a fixed size and read it in. Then write without memory overflow.
Such as code:
Copy code The code is as follows:
if (file_exists($file_path )) { //If the file exists
$handle = fopen($file_path, "r");
while (!feof($handle)) {
$content = fgets($handle, 4096) ; //Read a line
echo $content; //Output to the buffer, that is, php://stdout. After reaching the buffer setting value, it is passed to the browser by tcp for output. Generally, when 512 bytes are reached, it will be output to the browser through the network
}
fclose($handle);
}
But before outputting, it needs to be called once, @ob_end_flush(); it cannot be called in a loop, just call it once.
@ob_end_flush(); // Flush (send out) the output buffer content and close the buffer
File download:
content-type: //Download format, if the browser cannot parse the format, the download box will pop up
Copy code The code is as follows:
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Transfer-Encoding: binary");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
Header("Content-type : application/octet-stream"); //Response content type
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".filesize($filename). ' bytes') ;
Header('Content-Disposition: attachment; filename='.$filename); //HTTP response header
http://www.bkjia.com/PHPjc/327085.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327085.htmlTechArticlephp download files, such as txt files. The effect is that the download box that comes with the browser pops up and the save as operation appears. Sometimes memory overflow and timeout may occur. Timed out...
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