There may be a situation where you have to upload a file to the server using PHP. When uploading files you can’t upload big files to the server unless you change the default size. The default file upload limit in PHP is 2MB.  To upload big files to the server we have to change the maximum upload limit. We can change the upload limit by editing the php.ini file.

Uploading files to the server using PHP

Where is php.ini located?

To know the location of your php.ini file, we are going to create a PHP script. Open a text editor and type the below code:

And save it as phpinfo.php. Now move the phpinfo.php to the public_html. If you are using a local server, paste the phpinfo.php in the root folder where index.php is located.

We are using a local server. So to open phpinfo.php, go to your browsers URL section and paste the URL localhost/phpinfo.php and hit enter. A page will open which looks something like this:

php_ini_upload_limit

From that page, you can see where php.ini is located. In our case, the location is  /etc/php/7.0/apache2/php.ini.

Change maximum upload limit

Now that we have located the file, we can change the maximum upload limit. To open the file, right click on it and click Run as administrator. If you are using Linux, right click on the file and select  Edit as Administrator and enter your password.

Press ctrl + f. Type:

post_max_size

By default the post_max_size is 8M. Change the size according to your need.

post_max_size_walkytechy
Now again press ctrl + f. Type:

upload_max_filesize

By default the upload_max_filesize is 2M. You can change the maximum file size according to your need.

upload_max_filesize_walkytechy
Keep this in mind that the upload_max_filesize should not be more than post_max_size.

Now you can save the file and restart the Apache server.

If you are using Linux, just go to the terminal and type the below command:

sudo service apache2 restart

That is it. the server will be restarted and now you can upload big files using PHP.

Locate php.ini file and change the default file size limit
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *