Home >Backend Development >PHP Tutorial >How to set the file size using the readfile() function in PHP

How to set the file size using the readfile() function in PHP

墨辰丷
墨辰丷Original
2018-05-18 15:09:131578browse

This article explains how to use the readfile() function in PHP to set the file size through example code. Friends who need it can refer to it

Use the compressed package generated by PHP ZipArchive. Small compressed packages are It can be downloaded, but today I encountered a 404 error when it was over 150M. The first thing that came to mind was that the file size exceeded the default setting of PHP. There are two ways to modify it:

php.ini: memory_limit

memory_limit sets the memory limit. If you use readfile() to read files, it will be related to this. Just modify this value and save it, then restart php-fpm.

memory_limit = 128M

Finally remember: service php-fpm restart

ini_set

PHP ini_set is used Setting the value of php.ini will take effect when the function is executed. Then we can directly use it to modify the memory execution size. If some friends use virtual space, this function is a savior.

ini_set('memory_limit', '512M');


Full example:

set_time_limit(0);
ini_set('memory_limit', '512M');
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename=' . basename($zipfile));
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($zipfile));
ob_clean();
flush();
@readfile($zipfile);
unlink($zipfile);

Related recommendations:

php readfile() Modify file upload size case

readfile () function to set the php file size method

How to use the readfile() function in php to modify the setting of the file upload size

The above is the detailed content of How to set the file size using the readfile() function in PHP. 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