PHP tutorial 11: Uploading a file to the server

PHP has the ability to upload files on to the server using $_FILES superglobal.  $_FILES works a little differently from $_POST and $_GET in that each file creates an array of related elements that provide information about the uploaded file.

The provided information includes:

  • name: The file name
  • type: The file type (e.g. image/jpeg)
  • tmp_name: The temporary location of the uploaded file
  • error: An error code corresponding to error type (0 if no errors found)
  • size: The file size in bytes (e.g. 15454)

$_FILES , basically is a multidimensional  array, by using the global PHP $_FILES array you can upload files from a client computer to the remote server. The first parameter is the form’s input name and the second index can be either “name”, “type”, “size”, “tmp_name” or “error”.

Here are the array field values to handle uploads. Remember the first value [“file”] is user desired it can be anything user wants to refer.

$_FILES[“file”][“name”] – the name of the uploaded file

$_FILES[“file”][“type”] – the type of the uploaded file

$_FILES[“file”][“size”] – the size in bytes of the uploaded file

$_FILES[“file”][“tmp_name”] – the name of the temporary copy of the file stored on the server

$_FILES[“file”][“error”] – the error code resulting from the file upload

I am going to use all these above in the example where you could understand their use. Also note that, for security purpose, it is better to restrict user uploads. Allowing all type of uploads might turn out a security pitfall.

In the example iam going to use two scripts basically the first one as a HTML file that will be a form interface to select the desired photo to upload and the second as a PHP script that will do all the processing for a file upload. All the files uploaded will be saved permanently on to “upload‘  folder, so you need to create this folder before you execute the script.

HTML page to select the files to upload.

<form action="phpupload.php" method="post"
<label for="photo">User Photo:</label>
<input type="file" name="file" />
<input type="submit" value="Upload a Photo" />

Save the below code as (phpupload.php)

// check whether upload button is pressed

// all the superglobals to store file information and to verify a valid upload.
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000000))
// check for any error
  if ($_FILES["file"]["error"] > 0)
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
// display the file information
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

// Display the image

echo '<img src="upload/'.$file.' " height = 150 width = 150/>';

// check for already existing files

if (file_exists("upload/" . $_FILES["file"]["name"]))
      echo $_FILES["file"]["name"] . " already exists. ";
// saving the uploaded file permanently to the "upload" folder
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

  echo "file is incompatible, Plz try another image ";

echo "Choose image first";


Example:  Here I choose a JPEG image from my PC and then click the “upload” button.

As you can see image has been uploaded with all the details and a small preview as well.

The image has been successfully uploaded on to the folder “upload” which I have set in my PHP script.

Here is the complete review of all the work this PHP code can do:

  1. Keeping in mind all the needs, the PHP file upload script displays all the file(photo) details along with the picture that is being uploaded.
  2.  Allows you to upload images of gif, jpeg and jpg format. You can allow more number of files to be uploaded just by adding further file types in the $_FILES superglobal.
  3. If you try to upload nothing (just click the upload button without selecting any of the file) , you get a warning message.
  4. If you try to upload a text, pdf, .PNG file you get a error message saying “ File not compatible”;
  5. The PHP upload script also displays a small picture of the photo you just uploaded.
  6. Finally you are not allowed to upload same image twice

Also check out:

PHP Tutorial 1: Introduction for Beginners
PHP Tutorial 2: PHP Script Syntax and HTML File Compatibility
PHP Tutorial 3: Variables in PHP
PHP Tutorial 4: Control Statements
PHP Tutorial 5: Functions
PHP Tutorial 6: Form Processing
PHP Tutorial 7: $_POST and $_GET Functions
PHP Tutorial 8: Connecting PHP script to MySQL
PHP Tutorial 9: PHP insert statement
PHP Tutorial 10 : PHP Session