File uploading concept in PHP
Part – 2
-In previous part we learned about the basic requirements for uploading a file.
-Here in this part we are going to upload a file into a server.
Let us consider previous program and will add some logic for uploading a file into server as follows,
$name = $_FILES[‘file’][‘name’];
$type = $_FILES[‘file’][‘type’];
$tmp_name = $_FILES[‘file’][‘tmp_name’];
$size = $_FILES[‘file’][‘size’];
$location = “Uploads/”;
echo “$name <br> $type <br> $tmp_name <br> $size”;
echo “File Uploaded Successfully..!!”;
echo “Please Select a File”;
echo “There was an error in uploading file…!!”;
Okay!!File Uploaded Successfully..!!
-When we are executing the above program it will create the form elements, a button for choosing a file and a submit button for submitting form.
-Here in this program we are uploading a file in to a specified location & the location is declared in $location variable.
After choosing a file and when we click on upload,
-isset() function will returns true & !empty() function also returns true.
-Now move_uploaded_file() function will going to get executed as condition in nested if loop then it will return true if the file is uploaded successfully.
-When you are uploading a file, at first the file is going to get saved in a temporary location with a temporary name.
-Here we are giving the temporary name to the move_uploaded_file() function. Temporary name can be retrieved by using $_FILES[‘file’][‘tmp_name’].
– move_uploaded_file() function is used to upload a file given to it into a specified location.
tmp_name – It is the temporary name with location of the file to be
location – It is the location where the file you want to upload.
-So whenever move_uploaded_file() is going to execute it will return true after uploading the file. move_uploaded_file() will going to return false if the file cannot be uploaded.
-When an error occurred then $_FILES[‘file’][‘error’] will going to return 1.The main reason for getting error while uploading a file is that the size of the file exceeds the size limit. When an error occurred then $_FILES[‘file’][‘error’] will going to return 1.
-Hence the file is uploaded in to the server and you can check the uploaded file in the location you specified.