Javascript required
Skip to content Skip to sidebar Skip to footer

Php Upload Size Bigger Than Post Size

How to Restrict the Size of a File Upload with PHP

PHP



In this article, nosotros prove how to upload a file to a website and restrict the size of the file to any corporeality that you desire.

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

For any reason you lot want to implement this, PHP allows file sizes to exist restricted rather easily.

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

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

This form follows 20MB or less.

In actuality yous can brand it anything yous want. As long equally y'all may enough space on your hosting account, you can upload a file of whatever size. This means you tin can restrict files to 100MB or 200MB.

We will explain in full item 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. Nosotros'll get through this lawmaking since information technology creates the uploader, even though file brake isn't performed with HTML. File brake is later in the PHP code.

So the HTML shown above is very basic.

We create a form, a file upload course. We set the activeness equal to #upload. This allows us the form, once submitted, to spring to where nosotros take <a name="upload><a/>. We set the method equal to Post, considering it'southward a big amount of data and we don't desire 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. Information technology must be nowadays.

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

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

We then end the course.

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

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

PHP Lawmaking

There are 2 PHP blocks of code to brand this epitome upload work.

The first and the biggest block is shown below.

So we'll at present break down the PHP code shown higher up.

The $name variable stores the name of the file being uploaded. This includes the proper name of the file plus its extension. Then 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 last destination, which is the binder we want it transferred to. Instead, information technology gets a temporary. We then have to use the office, move_uploaded_file() to transfer the file using the temporary name to its last destination folder. Then, once more, 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 get-go step in restricting file size. We must first become the size of the file. This is done through the line, $size= $_FILES['file']['size'];

Ane 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 information technology is made upwardly of. For instance, consider a file that is roughly 2MB. This ways that the file size in bytes is about 2,097,152 bytes. No 1 actually knows files from the corporeality of bytes that information technology is composed. Instead nosotros know it mostly from either KB (kilobytes) or MB (megabytes).

So if you lot want to restrict file size to a sure value in KB or MB, then y'all have to know how to exercise the conversion. Depending on what arrangement y'all're using, a kilobyte is either grand bytes or 1024 bytes. It's close enough that we don't have to worry also much. Normally, you lot don't accept a precise cutoff. A megabyte is either 1000000 bytes or 1,048,576. In this commodity, 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 instance, 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 * ten= ten,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= twenty,971,520).

The variable $path makes the file uploaded to the Files of the Uploads folder. Subsequently on, we only have to append to the Files binder the name of the file then that we found the complete pathway to the file.

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

We then utilise the line, if (empty($proper name)), to determine once the submit push has been clicked whether the $proper noun variable contains anything or not. If the user simply pressed the 'Upload' push 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.

And so if the $proper name variable is empty, no file has specified by the user. Then nosotros 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 office, (move_uploaded_file($tmp_name, $path.$name)) to upload the file using the temporary proper noun created to its permanent location. Its permanent location volition exist its electric current folder, in the Files folders. Right now, y'all're viewing the website from, http://www.learningaboutelectronics.com/Manufactures/

And information technology's this if statement with the variable $size that allows usa to restrict uploads to less than the amount nosotros specify.

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

This completes the first cake of PHP code.

The second cake of PHP code is shown beneath.

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 and then shown below. Since the $tmp_name variable only holds a value when a file has been uploaded, information technology will be empty if nothing has been uploaded and comprise something if something has been uploaded.

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

Related Resource

hardingmarjohishe1947.blogspot.com

Source: http://www.learningaboutelectronics.com/Articles/How-to-restrict-the-size-of-a-file-upload-with-PHP.php