How to Change the Maximum File Upload Size of a PHP Script

PHP



In this article, we show how to change the maximum file upload size of a PHP script.

The maximum file upload size is the maximum size that a file can be when being uploaded to a site's server.

There are many reasons you would want to change the maximum file upload size, whether it is to increase it or decrease it.

Reasons you may want to increase it is because you may want users to be able to upload very large files such as full movies in PHP.

Reasons to decrease it is because you may want to restrict the file upload size users can use.

The setting that controls the file upload size in PHP is the upload_max_filesize setting.

By default, on many servers, this PHP upload_max_filesize is set to a maximum value of 32M (meaning 32 megabytes).

For moderately big files, this is enough. But if a user wants to upload very large files, such as hundreds of megabytes or even several gigabytes, this upload_max_filesize obviously needs to be increased.

So how do we do this?

So there are 2 ways really that we can do this.

One, we can go to the php.ini file on the server and change this setting manually. The php.ini file is the file on a server that allows you to change configuration settings for PHP. When changes are made to this php.ini file, it applies systematically to all pages on a server.

Second, you can change the configuration setting in the actual code with PHP code. When you do this, it applies only to the current PHP script that is being run. All other pages on the site do not have these affected changes.

To make this change, we use the PHP ini_set() function.

The ini_set() function takes in 2 parameters.

The first parameter is the configuration setting that you want to change, in this case, the upload_max_filesize size.

The second parameter is the value that you want to set this setting to.

So in the following code below, we set the maximum file upload size to 4000 megabytes.



So now that we've significantly increased the upload_max_filesize setting, we can increase much larger file sizes.

As you can see above, putting an "M" in the value parameter means megabytes. Putting a "K" in the value parameter means we are setting the value in kilobytes.

So this is how we can change the maximum upload file size in a PHP script.


HTML Comment Box is loading comments...