How to Restrict the Size of a File Upload with PHP

PHP












In this article, we show how to upload a file to a website and restrict the size of the file to any amount that you want.

You may restrict the file size of an upload due to users abusing or overusing memory space on your website.

For whatever reason you want to implement this, PHP allows file sizes to be restricted rather easily.

In the upload form above, the size of the file is restricted to 20MB or less.

If you try and upload a file larger than 20MB, the statement, "The size of the file must be less than 20MB in order to be uploaded" will be output.

This form follows 20MB or less.

In actuality you can make it anything you want. As long as you may enough space on your hosting account, you can upload a file of any size. This means you can restrict files to 100MB or 200MB.

We will explain in full detail how to implement size restriction of files below.

HTML Code

So below we will start with the HTML code needed to create the file upload form. We'll go through this code since it creates the uploader, even though file restriction isn't performed with HTML. File restriction is later in the PHP code.



So the HTML shown above is very basic.

We create a form, a file upload form. We set the action equal to #upload. This allows us the form, once submitted, to jump to where we have <a name="upload><a/>. We set the method equal to POST, because it's a large amount of data and we don't want the data appended to a URL. We want the data in an actual location, which is where we're uploading the image file to. The enctype statement is unique and needed for file uploads. It must be present.

The next line creates a file upload button. We name it "file".

The next line creates a submit button. Since it's an upload button, we name it "Upload".

We then end the form.

This is all the HTML code needed. The HTML simply creates the upload form.

The PHP code shown below is what actually adds functionality to the upload form so that it actually performs an upload.

PHP Code

There are 2 PHP blocks of code to make this image upload work.

The first and the biggest block is shown below.



So we'll now break down the PHP code shown above.

The $name variable stores the name of the file being uploaded. This includes the name of the file plus its extension. So if upload a file named mortage and its of the format pdf, the $name variable stores mortgage.pdf.

The $tmp_name stores the temporary name of the file. When we first upload the file, it isn't carried over directly to the final destination, which is the folder we want it transferred to. Instead, it gets a temporary. We then have to use the function, move_uploaded_file() to transfer the file using the temporary name to its final destination folder. So, again, the variable $tmp_name is very important for uploading the file.

The variable $size stores the size of the file name. This is the crucial first step in restricting file size. We must first get the size of the file. This is done through the line, $size= $_FILES['file']['size'];

One thing you have to know about the $size variable is that it stores the file size in unit bytes. The value of the file is in bytes.

Usually we don't consider files from the number of bytes it is made up of. For example, consider a file that is roughly 2MB. This means that the file size in bytes is about 2,097,152 bytes. No one really knows files from the amount of bytes that it is composed. Instead we know it mostly from either KB (kilobytes) or MB (megabytes).

So if you want to restrict file size to a certain value in KB or MB, then you have to know how to do the conversion. Depending on what system you're using, a kilobyte is either 1000 bytes or 1024 bytes. It's close enough that we don't have to worry too much. Normally, you don't have a precise cutoff. A megabyte is either 1000000 bytes or 1,048,576. In this article, we'll say a kilobyte is 1024 bytes and a megaybte is 1,048,576 bytes.

So, as examples, if we want to restrict a file upload to 100 kilobytes, this means that the maximum value of the $size variable would be 102,400 bytes (1024 * 100= 102,400 bytes).

As another example, if we want to restrict a file upload to 10MB, this means that the maximum value of the $size variable would be 10,485,760 bytes (1,048,576 * 10= 10,485,760).

Since in this uploader, we are restricting file sizes to less than 20MB, the maximum value of the $size variable would be 20,971,520 bytes (1,048,576 * 20= 20,971,520).

The variable $path makes the file uploaded to the Files of the Uploads folder. Later on, we just have to append to the Files folder the name of the file so that we establish the complete pathway to the file.

The line, if (isset($name)) checks to see whether the submit button has been clicked. If it has been clicked, the $name variable will be set.

We then use the line, if (empty($name)), to determine once the submit button has been clicked whether the $name variable contains anything or not. If the user simply pressed the 'Upload' button without specifying a file, the $name variable will be empty. If the user pressed the 'Upload' button while specifying a file, the $name variable will have something.

So if the $name variable is empty, no file has specified by the user. So we output the statement, "Please choose a file".

Else, if the $name variable is not empty and the file size is less than 20MB, we output the statement, "Uploaded". During this stage, we use the function, (move_uploaded_file($tmp_name, $path.$name)) to upload the file using the temporary name created to its permanent location. Its permanent location will be its current folder, in the Files folders. Right now, you're viewing the website from, http://www.learningaboutelectronics.com/Articles/

And it's this if statement with the variable $size that allows us to restrict uploads to less than the amount we specify.

So the file will be located at Articles/Uploads/Files folder.

This completes the first block of PHP code.

The second block of PHP code is shown below.



So this block of PHP code is much smaller.

If the $tmp_name variable isn't empty, the statement output is, "The file you uploaded is shown below." The link to the file is then shown below. Since the $tmp_name variable only holds a value when a file has been uploaded, it will be empty if nothing has been uploaded and contain something if something has been uploaded.

So this is all that is required to upload files, restrict the file size, and display them.



Related Resources

How to Upload Images to a Website Using PHP

How to Upload Videos to a Website Using PHP

How to Upload Files to a Website Using PHP