File uploading concept in PHP

File uploading concept in PHP
File uploading concept in PHP

Topic-50
File uploading concept in PHP
Part – 2

images-1-300x123-300x1234-300x123

DOC,PDF

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

Program:
<?php
$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”;

if(isset($name) ){
if(!empty($name)){
echo “Okay!!”;
if(move_uploaded_file($tmp_name,$location.$name)){
echo “File Uploaded Successfully..!!”;
}
}
else {
echo “Please Select a File”;
}
}else{
echo “There was an error in uploading file…!!”;
}
?>
php50-2-c
Output:
Strings.jpg
image/jpeg
C:\xampp\tmp\php78E3.tmp
48641
0
Okay!!File Uploaded Successfully..!!
php50-2-o

-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.
Syntax:
move_uploaded_file(tmp_name,location);

tmp_name – It is the temporary name with location of the file to be
uploaded.
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.

images-1-300x123-300x1234-300x123

DOC,PDF

Leave a Reply