Handle File upload and save file to server in PHP

Description

The following code shows how to handle File upload and save file to server.

Example


// w  ww.j  a  va  2s.co m
<html>
<body>
<?php
  //check for file upload
  if(isset($_FILES['upload_test']))
  {
    if($_FILES['upload_test']['error'] != UPLOAD_ERR_OK)
    {
      print("Upload unsuccessful!<br>\n");
    }
    else
    {
      //delete the file
      unlink($_FILES['upload_test']['tmp_name']);
      //show information about the file
      print("Local File: " .
        $_FILES['upload_test']['tmp_name'] . "<br>\n");
      print("Name: " .
        $_FILES['upload_test']['name'] . "<br>\n");
      print("Size: " .
        $_FILES['upload_test']['size'] . "<br>\n");
      print("Type: " .
        $_FILES['upload_test']['type'] . "<br>\n");
      print("<hr>\n");
    }
  }
?>
<form enctype="multipart/form-data"
  action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1024000">
<input name="upload_test" type="file">
<input type="submit" value="test upload">
</form>
</body>
</html>

The code above generates the following result.

Handle File upload and save file to server in PHP




















Home »
  PHP Tutorial »
    Form »




PHP Form
Form Demo