Tuesday, May 14, 2013

How to upload a file in PHP




today we gonne a see how to upload file in PHP

First 

Html File :

<form action="upload.php" method="post"  enctype="multipart/form-data"> //enctype is important
<input type="file" name="file" value="choose File" >/*you can limit selectable by specify extension like : 

accept="image/jpg, image/gif" for images jpg and gif*/
<input type="submit" value="upload" >
</form>


upload.php File :

<?php

    if (file_exists('upload/' . $_FILES['file']['name']))//test if file exist in upload directory
      {
      echo $_FILES['file']['name'] . ' already exists. ';//display a simple message if file exist in upload directory
      }
    else
      {
      move_uploaded_file($_FILES['file']['tmp_name'],'upload/' . $_FILES['file']['name']);/*upload file with it's original name in upload directory. */
      echo 'Stored in: ' . 'upload/' . $_FILES['file']['name']; /*display a simple message to the effect that file is uploaded successfully */

      }
?>

in the next tutorial i'm gonne show you how to upload multiple files Goodbye.




0 comments:

Post a Comment