Sunday, 9 June 2013

How to upload a word document or pdf file using php?

In web application development, many times you will need to upload files to server. For example, in case you are developing job portal, you will need to write the php script that will upload the resume to server.


You have to create the html form with below input element. This is required for browsing the file.

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

On submitting this form, the file that you have browsed will be stored on the server at temporary location.

You can access the file information in action script like below : -

<?php
  echo Name of file uploaded: " . $_FILES["file"]["name"] . "<br>";
  echo "Type of file uploaded : " . $_FILES["file"]["type"] . "<br>";
  echo "Size of file uploaded " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
  echo "Temp path where the file is stored " . $_FILES["file"]["tmp_name"];
?>

Now you can save the uploaded file from temporary location to other folder on the server.


      move_uploaded_file($_FILES["file"]["tmp_name"],"uploadfolder/" . $_FILES["file"]["name"]);

SO here we are saving the uploaded file in uploadfolder on the server. Before saving the file to said location you must check that file with that name does not exist there.

This is how you can upload a file on the server in PHP.

No comments:

Post a Comment