PHP is a popular server-side scripting language used to build dynamic web applications. One common task that web developers need to handle is uploading files to their web server. However, by default, PHP limits the file upload size to a maximum of 2MB. This can be a problem if you need to upload larger files, such as videos or high-resolution images.
In this article, we’ll discuss how to increase the file upload size in PHP, so you can handle larger file uploads on your website.
Step 1: Check your current upload limit
Before we can increase the file upload size, we need to check the current upload limit set in your PHP configuration file. To do this, create a new PHP file on your server, and add the following code to it:
1 2 3 | <?php phpinfo(); ?> |
Save the file as info.php and upload it to your web server. Then, open the file in your web browser by navigating to http://yourdomain.com/info.php. You should see a page with detailed information about your PHP installation.
Look for the “upload_max_filesize” and “post_max_size” values on this page. These values represent the maximum file size that PHP will allow you to upload. The default value for both is usually 2MB.
Step 2: Locate correct php.ini
In Step 1, You can also find the location of the php.ini configuration file.
For the PHP command line interface, you can find the correct php.ini file location, execute the following command:
php -ini | grep php.ini
Step 3: Update your PHP configuration file
Open the php.ini file in a text editor, and search for the following lines:
1 2 | upload_max_filesize = 2M post_max_size = 2M |
Change the values of these lines to the desired maximum file size that you want to allow for file uploads. For example, if you want to allow file uploads up to 50MB, you would change the lines to:
1 2 | upload_max_filesize = 50M post_max_size = 50M |
Save the php.ini file, and restart your web server for the changes to take effect.
Step 4: Test your new upload limit
After updating your PHP configuration file, you should test your new upload limit to make sure that it is working correctly.
Create a new PHP file on your web server, and add the following code to it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['file'])) { $file = $_FILES['file']; if($file['error'] == UPLOAD_ERR_OK && $file['size'] > 0) { // File uploaded successfully echo 'File uploaded successfully'; } else { // Error uploading file echo 'Error uploading file'; } } ?> <form method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="Upload"> </form> |
Save this file as upload.php, and upload it to your web server. Open the file in your web browser, and try to upload a file that is larger than the previous upload limit. If everything is working correctly, you should see a “File uploaded successfully” message.
Conclusion
Increasing the file upload size in PHP can be a simple process, but it requires some configuration changes to your PHP environment. By following the steps outlined in this article, you should be able to increase the maximum file upload size in PHP and start accepting larger files on your website.
However, it is important to note that allowing large file uploads can also have some potential drawbacks. Large files take longer to upload, which can slow down your website and put additional strain on your web server. In addition, large files can quickly consume your available disk space and bandwidth.
Therefore, it is recommended that you carefully consider the potential impact of increasing your file upload size and take appropriate measures to mitigate any potential issues.
Overall, by increasing the file upload size in PHP, you can provide a better user experience for your website visitors and enable them to upload larger files, which can be essential for many web applications.
5 Comments
Thank you very much for the invitation :). Best wishes.
PS: How are you? I am from France 🙂
Ok. I use translate and will be good ok?
hi 🙂 bross 🙂
For anyone that can read this, i found my php.ini working in another path, was
/etc/php/7.0/cgi/php.ini
Another paths i used in the past were
/etc/php/7.0/apache2/php.ini
/etc/php5/apache2/php.ini
Is there any command as
php -i | grep php.ini
that works for any Linux installation and not just CLI for looking for a file?
I have Debian 9 and have changed the upload_max_filesize to 20M but still can’t upload a 4 megs file, an mp4, via WordPress installed in ISPConfig.
It continues saying that the size of the file exceeds the allowed size of the site.
I have changed the upload_max_filesize value in /etc/php/7.0/apache2/php.ini and also in /etc/php/7.0/cli/php.ini
Any ideas of how it didn’t worked? Maybe there’s something i don’t realize right now.