How to modify the upload size limit in php

PHPz
Release: 2023-04-24 15:39:04
Original
2017 people have browsed it

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.

  1. Find your php.ini file. You can find the location of the php.ini file by entering the following command in the terminal:
php --ini
Copy after login

This command will display the path to the php.ini file. Please note the path.

  1. Open the php.ini file with a text editor. You can use the following command to open the php.ini file:
sudo nano /path/to/php.ini
Copy after login

Please replace /path/to/php.ini with the path you just recorded.

  1. Modify the upload file size limit. Search for the following two variables in the php.ini file:
upload_max_filesize 
post_max_size
Copy after login

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
Copy after login

Please note that the size units here are divided into MB and KB, and are case-sensitive.

  1. Save and close the php.ini file. In the terminal, press Ctrl X, then Y, and finally Enter to save and close the php.ini file.
  2. Restart the web server. After modifying the php.ini file, it is best to restart the web server so that the modifications will take effect. You can restart the Apache server through the following command:
sudo service apache2 restart
Copy after login

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.

  1. Open the .htaccess file. If you don't have a .htaccess file in the root directory of your website, you can create one by creating a new .htaccess file in a text editor and copying the following code into it:
<IfModule mod_rewrite.c> 
    RewriteEngine On 
</IfModule>
Copy after login
  1. Add the following directive in the .htaccess file:
php_value upload_max_filesize 100M 
php_value post_max_size 100M
Copy after login

The 100M here represents the upload file size limit you want to set.

  1. Save and close the .htaccess file.

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.

  1. Add the following code in the PHP code:
ini_set('upload_max_filesize', '100M'); 
ini_set('post_max_size', '100M');
Copy after login

The 100M represents the upload file size limit you want to set.

  1. Save and close the PHP code file.

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template