At present, more and more webmasters have created a special form that allows users to upload files to their websites, consolidating the relationship and shortening the distance between website owners and loyal readers to a large extent. The following detailed guidance tells you how to create such a form with the help of PHP scripts, helping users upload files using PHP to your site easily and safely.
How Does This Work?
Before displaying the related PHP coding stuff, we firstly want to show you the process to upload files to your web server. Generally speaking, there are 6 steps in total.
- Step One – People open up the exclusive page that contains a form coming with a submit button, a search box, and a browse button.
- Step Two – People click on the button of browse and choose the target file on your website.
- Step Three – After the whole uploading process is completed, people then need to hit the button for submitting.
- Step Four – Initially, the uploaded file is uploaded into a temporary folder. The phpinfo.php indicates the directory that is used for file uploads as upload_tmp_dir.
- Sep Five – The PHP acts as the uploading handler to check out whether the selected file arriving. If the answer is yes, then it copies the data and relocates it to a targeted and intended directory.
- Step Six – The PHP informs users of the success of file uploading.
How to Upload Files Using PHP?
First of all, you need to create an uploading form for users that allows them to select a file to add. To do this, you’d better make use of the multi-part value for the enctype property that decides the data-type when submitting. Note that the code of enctype=”form-data” indicates that the form requires binary data to be uploaded.
In addition, you’ll also need the attribute of type=”file” within the tag of input to provide a place for typing the file name, to display the browse button next to the search field, and to open up the box allowing users to select the files they want to upload.
There is a special note that the appearance of the browse button might be different due to different browsers, but the function is still the same.
Then, the next step is to create the upload script by adding the related code into the uploader file. In fact, after this file is executed, the uploaded file is placed in a temporary section. To save the information related to the file, you need to use the PHP variable named as $_FILES.
Here is the code for the uploader.php script.
For the sake of safe and security, you’d better set some restrictions concerning about uploading files to your website. For instance, you only allow users to upload image files with the formats of gif, jpeg, jpg, pjpeg, x-png, and png, and the size of any file cannot excess 15 kb.
The last step is to save the uploaded file. In fact, all the lines of code showcased above are used to create the copy of the uploaded file that will disappear with the ending of the script. Therefore, you have to copy it and relocate it to your wanted directory. You can combine the coding stuff of the second and the third step.