At times while uploading files in php our files are way too large than the default php max upload size which is 2mb for most of you. Therefore this post will be covering on increasing the maximum upload size with the php.ini and additionally I will also tell you how to also do it using the .htaccess file.
if have access to your servers php.ini file and you wish to change the maximum upload size for all the websites then you shall go for the php.ini method else scroll down to the .htaccess method.
Table Of Contents
Increasing PHP Maximum Upload Size By Editing The php.ini File
Basically what we need to do use is to locate the php.ini and then update the upload_max_filesize and post_max_size to the values that you need to. Suppose your php script needs to upload a file which is 100 megabytes in size then you may set the value of upload_max_filesize to 100M and post_max_size to 101M.
Now that you know what we need to do, let’s get started!!
Locating / Finding The php.ini File
Before editing the php.ini file we first need to know that where is it located, right? If you already know the location of your php.ini file it’s great you may skip reading this and directly scroll down to updating the file, But if you don’t know the location then I will tell you how to find the location of your php.ini file.
For finding the file you need to create a new .php file name it to anything you want but for this post, we will keep the file name to phpinfo.php And make sure you create the file in the home directory so that it would be accessible from a browser. Once you create the file just paste the below code into the file and save it.
<?php phpinfo(); ?>
Now that you have saved the file open it from the browser. So if you did as I said and you have saved a the phpinfo.php file into the home directory you can access it from http://example.com/phpinfo.php. You will see your server php configuration on that page.
like this –
Look for loaded configuration file it will tell you the path to your php.ini which is currently in use. So now that you have got the location of your php.ini you can now edit it to increase the php max upload file size limit.
Editing The php.ini File
You can use FTP, SFTP, SSH or anything you have to edit the php.ini file. If you are using FTP or sftp I would recommend you to use FileZilla. I personally prefer using CodeTasty for managing files on my server.
Now open the php.ini file and search upload_max_filesize and set it to as many M you need. I mean set it to maximum upload size that you want to. Next search for post_max_size and set it to a little greater than you need to. Suppose if you need 150M set it to 151 to ensure that a file uploads successfully everytime.
Mostly you will find the upload_max_filesize around line number 809 and post_max_size around line number 656 in your php.ini file.
Restarting Your WebServer
Changes that you have made to the php.ini are not applied yet because your web server was started before you updated the file and therefore you need to restart your web server so that the updated php.ini file gets loaded and upload size is increased.
Which web server Software are you using? Do the steps according to the web server you are using.
if you are using the latest version of Ubuntu or Debian this command will work for you –
sudo service apache2 restart
If you are using some other operating system then you can read my post on how to restart apache.
Mostly this command will work for all of you.
sudo service nginx restart
In case if it doesn’t work read my post on how to restart nginx.
And that’s it you have successfully increased your php max upload size. So now you can enjoy uploading bigger files on your server.
Now Let’s see how to increase the php max upload size using the .htaccess file.
Using .htaccess File For Increasing PHP Max Upload File Size
Using the .htaccess method for increasing the upload size limit is easier than updating the php.ini file. For Most of you there might already be a .htaccess file already created so you just need to make a few changes to it but in case if .htaccess file is not created already then you may create a new file and then set the name to .htaccess.
Editing The .htaccess File
Once you have the htaccess file insert the following lines in it.
php_value upload_max_filesize xxM php_value post_max_size xxM
Set the xx to the number of megabytes to the max upload file size you want to. Once you have done it just save the file and you are good to go now. You don’t need to restart apache or restart nginx for the changes to take place the changes will take place instantly.
Common Problem Solution
If you already had a .htaccess file and you have just updated it then you might not face this issue but in case if you have created the .htaccess file then there are chances that you might get a 500 Internal server error.
In case if you just need to verify that if the issue is caused due to the .htaccess so just try renaming the file and then reload the page if you don’t see the issue it means that the issue is due to the .htaccess file but if the issue still exists it means that you have messed up somewhere.
For solving this issue you need to set All directory override to All instead of none in the apache2.conf or httpd.conf file.
That’s it for today I hope you have successfully increased the max upload file size of php but in case if you have faced any issue then let me know in the comments section below I would be glad to help you out.
And yes please do share this post with your other developer friends on facebook, twitter, linkedin, etc.