PHP Tutorial: How to Upload Files via PHP ?

Introduction

"How to upload files on web server?" is the most common question that arises in the path of every web developer, either in learning phase or in practical period (While working on any project). It is just as simple as fetching data submitted via any HTML form. But this time, data is large in size (for example: Image, video etc.) and needs to be handled in proper way by indicating it in form itself.

Creating an HTML Form

I hope, You're talented enough to create basic HTML form, But, In this case, We are going to add one more attribute in our starting "<form>" tag i.e. by setting/adding enctype="multipart/form-data" . This attribute tells the browser that our form may also upload some big files along with text data and then, browser will behaves as accordingly and It will submit such data to our php script in proper way. The HTML code of Such form is shown below.

<form enctype="multipart/form-data" method="post" action="#">
<label>File: </label><input type="file" name="filedata" /><br />
<input type="submit" name="submit" value="Upload File" />
</form>

Developing PHP Script to Upload Files

Now, we are going to write our php script, which is going to handle data submitted via form in an proper manner, So, Files can be uploaded successfully to our web server.

  • First, We will write some PHP code to display details of uploading file

<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " KB<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?> 

In the above code, $_FILES["file"]["tmp_name"] will display the temporary name of file uploaded on our server and $_FILES["file"]["name"] will display original name.

  • Now, We will see PHP code that will do the Uploading Task

<?php
$tmp_name=$_FILES["file"]["tmp_name"];
$filename=$_FILES["file"]["name"];
$target="folder/".$filename;

if (file_exists($target))
      {
      echo $filename . " already exists. ";
      }
    else
      {
      	move_uploaded_file($tmp_name, $target);
      	echo "File Stored in: ".$target;
      }
?>

Combining HTML and PHP together

Now, we will merge all the code together in an single page.

<?php
	if(isset($_POST['submit'])){

//Displaying file details script begins here
		if ($_FILES["file"]["error"] > 0)
		  {
		  echo "Error: " . $_FILES["file"]["error"] . "<br />";
		  }
		else
		  {
		  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
		  echo "Type: " . $_FILES["file"]["type"] . "<br />";
		  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " KB<br />";
		  echo "Stored in: " . $_FILES["file"]["tmp_name"];
		  }
//Uploading script begins here
		$tmp_name=$_FILES["file"]["tmp_name"];
		$filename=$_FILES["file"]["name"];
		$target="folder/".$filename;
		
		if (file_exists($target))
		      {
		      echo $filename . " already exists. ";
		      }
		    else
		      {
		      	move_uploaded_file($tmp_name, $target);
		      	echo "File Stored in: ".$target;
		      }
	}else{
//HTML form Begins Here		
	?>
	<form enctype="multipart/form-data" method="post" action="#">
	<label>File: </label><input type="file" name="filedata" /><br />
	<input type="submit" name="submit" value="Upload File" />
	</form>
	<?php
		
	}
?>

End Points

It is very easy to develop and understand mechanism of uploading file via php, If you're having trouble in understanding this, then, you can submit your queries in comment section but I'll recommend you to advance your knowledge in arrays first. They will help you in advancing your knowledge in many areas.

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working