File Upload in PHP

$_FILES is a superglobal variable in PHP. It consists of 5 variable:

  •  $_FILES[‘file’][‘tmp_name’] – the uploaded file in the temporary directory on the web server.
  •   $_FILES[‘file’][‘name’] – the name of the file which is uploaded.
  •   $_FILES[‘file’][‘size’]-the size of the uploaded file.
  •   $_FILES[‘file’][‘type’]-the MIME type of the uploaded file.
  •   $_FILES[‘file’][‘error’]-the error will be shown.                                                                                        
Step 1: Create a form. While creating a form some rules must be follow:
  •   The form must uses a method=”post”.
  •   The form also need the attribute: enctype =”multipart/form-data”. It specifies which content is to be used while submitting the form.
  •   NOTE: In <input> tag “file” must be given.
<html>


   <body>



      <form action=”image.php” method='POST' enctype='multipart/form-data'>

         <input type='file' name='image' />

         <input type='submit' name='submit'/>

      </form>



   </body>

</html>


 Step 2: Create image.php


<?php

if(isset($_POST['submit']))

{

move_uploaded_file($_FILES['image']['tmp_name'],'../user/'.$_FILES['image']['name']);

?> // upload the file in user folder.

 <img src='<?php echo $_FILES['image']['name'];?>' width='300px' height='200px'>//preview your image

<?php

}

?>
Last updated:3/15/2018 5:54:24 AM

1 Comments

Leave Comment