Home  >  Article  >  Backend Development  >  How to modify file size limit through PHP

How to modify file size limit through PHP

PHPz
PHPzOriginal
2023-04-04 09:13:31805browse

When performing file upload or download operations, it is easy to encounter the problem that the file size exceeds the server limit. At this time, you need to modify the file size limit of the server to ensure that larger files can be uploaded or downloaded. This article describes how to modify the file size limit through PHP.

1. Modify the php.ini file

Modifying the php.ini file is the most direct way. php.ini is the PHP configuration file, including all configuration options for PHP Script Language. You need to confirm the location of php.ini before modifying it. This usually requires contacting the server administrator or hosting provider. If PHP is installed independently, php.ini is usually in the /etc/php.ini or /usr/local/lib/php.ini directory.

The method to modify the php.ini file is as follows:

  1. Open the php.ini file and find the file limit parameters by searching for "upload_max_filesize" and "post_max_size".

    upload_max_filesize=2M
    post_max_size=3M

    Among them, upload_max_file_size limits the size of a single uploaded file, in bytes. You need to modify it to the size you need. For example, if you modify it to 10M, you need to modify it to the following code:

    upload_max_filesize=10M

    Similarly, post_max_size is the maximum value that limits a file upload and needs to be modified according to actual needs. For example, modify it to 20M. It needs to be modified to the following code:

    post_max_size=20M
  2. Save the modified php.ini file and restart Apache for the modification to take effect.

2. Modify the .htaccess file

If you do not have permission or do not want to modify the php.ini file, you can also modify the file size limit by adding code to the .htaccess file of the website. Open the .htaccess file and add the following code:

php_value upload_max_filesize 10M
php_value post_max_size 20M

At this time, the upload file size limit is modified to 10M, and the maximum total size of the file uploaded once is 20M.

It should be noted that this method is only valid on the Apache server, and the Apache module of PHP needs to be turned on to take effect.

3. Modify through code

If the above two methods cannot be used, you can also modify the file size limit through PHP code. Just add the following code before uploading the file:

ini_set('post_max_size', '20M');
ini_set('upload_max_filesize', '10M');

Among them, 20M represents the maximum total size of an upload, and 10M represents the maximum size of a single uploaded file.

This method of code is the most flexible, but it requires modifying the program code and is not suitable for people who are not familiar with the server.

Summary

On a server that supports PHP, you can modify the file size limit by modifying the php.ini or .htaccess file, or by using PHP code. By modifying the limit, you can get a better website user experience. At the same time, you need to pay attention to the server hardware level to ensure that the modified limit size is supported.

The above is the detailed content of How to modify file size limit through 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