When using PHP for file upload, you may encounter the problem of upload file size limit. By default, PHP will limit the size of uploaded files to 2MB, which is not enough for some applications that need to upload larger files. So how to modify the upload file size limit? This article will introduce you to how to modify the upload size limit in PHP.
1. Modify the php.ini file
Modifying the php.ini file is the most common solution. php.ini is the PHP configuration file. It not only stores the upload file size limit, but also stores other PHP configuration information. If PHP is already installed on your server, you can follow the steps below to modify the php.ini file.
php --ini
This command will display the path to the php.ini file. Please note the path.
sudo nano /path/to/php.ini
Please replace /path/to/php.ini with the path you just recorded.
upload_max_filesize post_max_size
These two variables represent the upload file size limit and the POST request data size limit respectively. Change their values to the size you need. For example, if you want to increase the upload file size limit to 100MB, you can modify it like this:
upload_max_filesize = 100M post_max_size = 100M
Please note that the size units here are divided into MB and KB, and are case-sensitive.
sudo service apache2 restart
2. Modify the .htaccess file
If you do not have permission to access the php.ini file, or you do not want to modify the global upload file size limit, then you can change the upload file size limit by modifying the .htaccess file. The .htaccess file is a file used to control directory configuration in the Apache server. It can perform some configurations on the site in the current directory, including upload file size limits.
<IfModule mod_rewrite.c> RewriteEngine On </IfModule>
php_value upload_max_filesize 100M php_value post_max_size 100M
The 100M here represents the upload file size limit you want to set.
3. Set limits in PHP code
In some cases, you may need to dynamically modify the upload file size limit. In this case, you can configure it in PHP code.
ini_set('upload_max_filesize', '100M'); ini_set('post_max_size', '100M');
The 100M represents the upload file size limit you want to set.
Summary
Modifying the upload file size limit is necessary for some applications that need to upload larger files. This article introduces three methods to modify the upload file size limit, including modifying the php.ini file, modifying the .htaccess file and setting limits in the PHP code. Different methods are suitable for different situations, and you can choose the appropriate method according to your actual situation.
The above is the detailed content of How to modify the upload size limit in php. For more information, please follow other related articles on the PHP Chinese website!