How to Upload Files to a Website Using PHP

PHP













In this article, we show how to upload any type of file to a website using PHP.

This can be any type of file at all, such as a microsoft word document (.doc or .docx), a text file (.txt), a video file (.mov), an html document (.html), an image file (.jpg or .png), etc. There are many, many different types of file extensions. This upload form doesn't discriminate between any type. For the form above, however, to protect this site, php files, asp files, javascript files, and xml files have been blocked from being uploaded.

With this upload feature, you upload files to whatever folder you specify in the code. Once you have the file selected to upload, you press the 'Upload' button. The file is then uploaded to the folder that you specify. You can then display this image by the HTML <a href> tag.

This is exactly what is done above.

We created a file upload box in HTML and then we added PHP to give functionality to the textbox so that an actual upload can occur. Once we have the image uploaded, we can then display the image through some simple PHP code. So if you upload an image above, then click the 'Upload' button, you will see the image you uploaded right below.

So below we go over how to do this.


HTML Code

So below we will start with the HTML code needed to create the file upload form.



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 this is obviously a big block of code. We'll explain it all now below.

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 $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, 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/

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 $name isn't empty, the statement output is, "The file you uploaded is shown below." The link to the file is then shown below.

So this is all that is required to upload files and how to display them. It isn't particularly hard once you know what you're doing.

You can always modify the code to fix your needs. For example, you may only want to accept certain formats of files, such as .pdf, .doc., and .doc extensions. In this case, you will have to modify the code so that it only accept those formats. We show this modified code at the following page, How to Upload Files with certain file extensions to a Website Using PHP

If you want to see how to restrict the size of the file being uploaded, see How to Restrict the Size of a File Upload with PHP.


Related Resources

How to Upload Images to a Website Using PHP

How to Upload Videos to a Website Using PHP

How to Insert Images into a MySQL Database Using PHP

How to Insert Videos into a MySQL Database Using PHP