Spread the love

In this tutorial, we will learn the basic concept of how Php file upload to an online server.We can upload any kind of files like images of type  JPG, PNG, GIF etc, and  PDF  file, TEXT files, PRESENTATION, NOTES, EXECUTABLE and a variety of other files to an online server using Php and HTML.To upload files into online server we have to follow the following two-step

  1. Create HTML form
  2. Page for Php script

In the above HTML form, we mentioned the enctype attributes which specify the encoding method which is necessary for uploading maximum size binary data.You might be known that HTML provides three type of encoding methods.

i.application/x-www-form-urlencoded (the default)



Sometimes we don’t care about enctype attribute but sometimes its use becomes very compulsory when our form contains file element. otherwise, we can’t upload the file to an online web server.

2.the upload.php file contains a complete script along with which store our image in our upload folder permanently and also having image validation like size, type to confirm the accuracy of the image to upload to the online server etc.

Code Explanation:

In the above code  $_FILES[] is a superglobal array or variable which can access from any scope which actually handles file element.

$_FILES[‘file’][‘name’] attribute handle the original name of the file upload by user through form

$_FILES[‘file’][‘tmp_name’] will contain the temporary name of the file on the server until user process it.

$_FILES[‘file’][‘type’] represent the MIME type of the file

$FILES[‘file’][‘size’]represent the size the file

$_FILES[‘file’][‘error’] show the error when occurring during uploading.

And we use explode function to convert the file into an array it takes two parameter separator ‘.’ And string.

the strtolower function use to convert uppercase extension to lower case .and end() is used to return end of the file

.the function in_array() is used to search for a specific value in the array.

You may be noted that when sometime we upload same type and same size image it overwrites the old one if exist in folder.so for this purpose we use uniqid()to assign a unique id to a file to prevent overwriting.