How to upload a file

An HTML form for uploading a file

      <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="file1"><br>
        <input type="submit" value="upload"><br>
      <form>
      

The browser display of the HTML

browser upload image

Elements of the nested arrays in the $_FILES array

Index Description
'name' The original name of the uploaded file.
'size' The size of the uploaded file in bytes.
'tmp_name' The temporary name of the uploaded file on the web server.
'type' The MIME type of the uploaded file as sent by the user’s browser.
'error' The error code associated with the file. Common values are UPLOAD_ERR_OK (no error), UPLOAD_ERR_INI_SIZE (file was too large), and UPLOAD_ERR_PARTIAL (upload was not completed).

A function to save an uploaded file

Function Description
move_uploaded_file($tmp, $new) Moves an uploaded file from its temporary location to a permanent location. If successful, returns TRUE.

PHP for working with an uploaded file

      $tmp_name = $_FILES['file1']['tmp_name']; 
      $path = getcwd() . DIRECTORY_SEPARATOR . 'images'; 
      $name = $path . DIRECTORY_SEPARATOR . $_FILES['file1']['name']; 
      $success = move_uploaded_file($tmp_name, $name); 
      if ($success) {
        $upload_message = $name . ' has been uploaded.';
      }
      

Description

Back