PHP - Storing and Using an Uploaded File

Introduction

Once a file has been successfully uploaded, it is automatically stored in a temporary folder on the server.

To use the file, or store it on a more permanent basis, move it out of the temporary folder.

You do this using PHP's move_uploaded_file() function, which takes two arguments: the path of the file to move, and the path to move it to.

You can determine the existing path of the file using the tmp_name array element of the nested array inside the $_FILES array.

move_uploaded_file() returns true if the file was moved successfully, or false if there was an error.

if (move_uploaded_file($_FILES[" photo" ][" tmp_name" ]," /home/matt/photos/photo.jpg" )) {
    echo " Your file was successfully uploaded." ;
} else {
    echo " There was a problem uploading your file-please try again." ;
}

Example

In this example, you create a script that displays a form allowing the user to upload a JPEG photo, which is then displayed to them in the page.

First, save the following script as photo_upload.php in your document root folder:

<html>
<head>
      <title>Uploading a Photo</title>
</head>
<body>
  <?php
   if (isset($_POST[" sendPhoto" ])) {
    processForm();
   } else {
    displayForm();
   }

   function processForm() {
    if (isset($_FILES[" photo" ]) and $_FILES[" photo" ][" error" ] == UPLOAD_ERR_OK) {
      if ($_FILES[" photo" ][" type" ] !="image/jpeg" ) {
        echo " <p>JPEG photos only, thanks!</p>" ;
      } elseif (!move_uploaded_file($_FILES[" photo" ][" tmp_name" ]," photos/" . basename($_FILES[" photo" ][" name" ]))) {
        echo " <p>Sorry, there was a problem uploading that photo.</p>" . $_FILES[" photo" ][" error" ];
      } else {
        displayThanks();
      }
    } else {
      switch($_FILES[" photo" ][" error" ]) {
        case UPLOAD_ERR_INI_SIZE:
          $message ="The photo is larger than the server allows." ;
          break;
        case UPLOAD_ERR_FORM_SIZE:
          $message ="The photo is larger than the script allows." ;
          break;
        case UPLOAD_ERR_NO_FILE:
          $message ="No file was uploaded. Make sure you choose a file to upload." ;
          break;
        default:
          $message ="Please contact your server administrator for help." ;
      }
      echo " <p>Sorry, there was a problem uploading that photo. $message</p>" ;
 }
}

function displayForm() {
?>
   Uploading a Photo
   <p>Please enter your name and choose a photo to upload, then click Send Photo.</p>
   <form action="photo_upload.php" method="post" enctype="multipart/form-data">
     <div style="width: 30em;">
       <input type="hidden" name="MAX_FILE_SIZE" value="50000" />
       <label for="visitorName">Your name</label>
       <input type="text" name="visitorName" id="visitorName" value="" />
       <label for="photo">Your photo</label>
       <input type="file" name="photo" id="photo" value="" />
       <div style="clear: both;">
         <input type="submit" name="sendPhoto" value="Send Photo" />
       </div>

     </div>
   </form>
<?php
}

function displayThanks() {
?>
   Thank You
   <p>Thanks for uploading your photo<?php if ($_POST[" visitorName" ])
    echo " ," . $_POST[" visitorName" ] ?>!</p>
   <p>Here's your photo:</p>
   <p><img src="photos/<?php echo $_FILES[" photo" ][" name" ] ?>" alt="Photo" 
/></p>
<?php
}
?>

 </body>
</html>

Give your Web server user the ability to create files in this folder.

cd /path/to/document/root
chmod 777 photos

Related Topic