Saturday 29 March 2014

Uploading Files in Php


uploading files in php
Uploading Files In Php Programming Language. Uploading Files The Basics In Php Programming Language. Uploading Files Is very very Popular Topic because lots of websites now deals with files uploads it may be example uploading an images, Uploading documents, Even Uploading something needs to be proceeded at some point. So, how we do we handle file upload in php. Now its relatively easy handle file uploads in php. For the purpose of this tutorials You can see screenshot 1 script file name upload.php and directory name uploads/ folder. We will work with upload.php. Now the purpose of this videos its just simply how we take a file allow the user to select file from the computer and then send it to specific directory. Here we are not not looking to the security for uploading of the files. So, This codes are not safe to use at web-server because in actual facts the user will be upload any file. For example user can upload .php file extension file an they could miss-around with your server. So, In this tutorials we just learning how to uploads files but not its security.

Code For Uploading Files The Basic In Php


<?php

$name = $_FILES['file']['name'];

$size = $_FILES['file']['size'];

$type = $_FILES['file']['type'];



$tmp_name = $_FILES['file']['tmp_name'];



if (isset($name)) {

if(!empty($name)) {

echo 'OK.';

}else {

echo 'Please Choose a file.';

}

}



?>



<form action = "upload.php" method="POST" enctype="multipart/form-data">

<input type="file" name="file"><br><br>

<input type="submit" value="Submit">

</form>

Output (Screenshot2)

uploading files in php
Screenshot1
upload files in php
Screenshot2
Upload Files in php
Screenshot3
uploading files in php
Screenshot4
uploading files in php
Screenshot5
uploading files in php
Screenshot6
uploading files in php
Screenshot7

We will start with create form with form action upload.php because out php script is in same file. method is going to be POST remember we are sending large amount of data so method be Post. There is one addition attribute you need to apply in this form tag encrypt this is a way the data in form encoded when its send we need to supply this multipart/form-data So this is basically allow us to process the form and enable file upload we are sending alot much amount data and this is  encrypt this just tell us when we process on it it tells how we gonna encoded the data. So, Now we allow the user to upload the file we use input type tag file which will create browse button with name called file Then we create Submit Button to it.

In this article we are not including form security in another article we will only allow .jpg file to be uploaded. When the file is upload with php script we will grape file name, size of file, temporary location of the file. 



<?php

$name = $_FILES['file']['name'];

$size = $_FILES['file']['size'];

$type = $_FILES['file']['type'];



$tmp_name = $_FILES['file']['tmp_name'];



if (isset($name)) {

if(!empty($name)) {

echo 'OK.';

}else {

echo 'Please Choose a file.';

}
}



?>

In piece of php code we will grape the file name $name = $_FILES['file']['name']; This will show us the file name, $size = $_FILES['file']['size']; This will show us the file size, Then $type = $_FILES['file']['type']; This will show us the file type, $tmp_name = $_FILES['file']['tmp_name']; This will show the temporize location the file had been uploaded to the server. $error = $_FILES['file']['error']; This will show any error in uploading the files to the server.

Then i will check whether the file is uploaded we can access all information of file which is uploaded to the server but in this example i will only just check whether file is uploaded or not so will check with isset function. You can see output screenshot if file is not choose it shows message to the user please choose a file and if file is uploaded to the server you can see the message ok.


Code for Uploading File to the Directory 



<?php

$name = $_FILES['file']['name'];

$size = $_FILES['file']['size'];

$type = $_FILES['file']['type'];



$tmp_name = $_FILES['file']['tmp_name'];



if (isset($name)) {

if(!empty($name)) {





$location = 'uploads/';



if(move_uploaded_file($tmp_name, $location.$name)) {

echo 'Uploaded!';

}else {

echo 'Please Choose a file.';

}

}



?>



<form action = "upload.php" method="POST" enctype="multipart/form-data">

<input type="file" name="file"><br><br>

<input type="submit" value="Submit">

</form>

Output

uploading php file
Screenshot6
uploading files in php
Screenshot7


In this example we are uploading file to temporarily location to particular directory which we had uploads folder as you can see screenshots6 File is saved in uploads folder. In screenshot7 you can see the file url it is saved in uploads folder. In this way we can successfully uploaded a file. We used move_uploaded_file function with passing two parameters $tmp_name is variable in which temporarily file location and the $location where we want to move the file with $name variable which contain the name of the file with extension of the file.


No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates