Page 1 of 1

PHP - Uploading a File Difficulty Level: Easy Rate Topic: ***** 1 Votes

#1 sparkart   User is offline

  • D.I.C Addict
  • member icon

Reputation: 115
  • View blog
  • Posts: 696
  • Joined: 16-February 09

Post icon  Posted 02 August 2009 - 06:32 AM


This tutorial is aimed at beginners. It uses basic functions so you shouldn't have any difficulty. The purpose of the tutorial is to teach you how to upload files using PHP and HTML forms.

The following requirements should be met before starting this tutorial:
  • HTML & PHP Knowledge
  • PHP-enabled host
  • Ability to upload

To simplify the process this tutorial will be divided into steps, beginning with the creation of the upload form.


Create a new HTML file and add the following code snippet:

<form action="fileUpload.php" method="post" 
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Upload" />


The enctype attribute of the form defines the type of content we are working with. By using multipart/form-data for this attribute we are able to work with binary data (which we will need for the file to be uploaded).

In the first input tag you notice the following attribute: file. This allows us to process a file for input. That way, the user can select the file they wish to send.

The action attribute of the form is: "fileUpload.php". That is the file that will be processed when this form is submitted (when the "Upload" button is clicked).


The next step involves creating the upload script. Create a PHP file and call it: fileUpload.php. Note that you do not have to give it this name, but it must match the name in our HTML document's form action.

Add the following code snippet in your PHP file:
// Check for any errors.
if ($_FILES["file"]["error"] > 0)
    print("Error: ". $_FILES["file"]["error"]);

    // Display statistics.
    print("Uploaded: " . $_FILES["file"]["name"]. "<br />");
    print("Type: " . $_FILES["file"]["type"] . "<br />");
    print("Size: " . $_FILES["file"]["type"] . "<br />");

    // Save file to permanent location.
    "uploads/" . $_FILES["file"]["name"]);

move_uploaded_file(...) is required because our file is uploaded to a temporary location. Once the script ends, it will be deleted. By using move_uploaded_file(...) we move it to a permanent location relative to the file's name.


We can do some modifications to our upload script like setting file type and size restrictions. To do this we'll be comparing the following superglobals with certain values:

Here is a small snippet that demonstrates how to check if the file is a gif file.
if (_$FILES["file"]["type"] == "image/gif")
    print("The file is a gif image.");

This snippet checks if the file size exceeds 1 megabyte:
if (_$FILES["file"]["size"] < 1000000)
    print("File size: " . $_FILES["file"]["size"] . ". <br />");

    print("The file is too big. <br />");

Is This A Good Question/Topic? 5
  • +

Replies To: PHP - Uploading a File

#2 javafreak   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Posted 28 August 2011 - 04:21 AM

thanks aloat very great code !!
Was This Post Helpful? 0
  • +
  • -

#3 goku87   User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 17-October 09

Posted 24 October 2011 - 08:40 AM

Just a heads up, you are missing a closing bracket after "error" on line 3.
Was This Post Helpful? 0
  • +
  • -

#4 James_Smith   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 20-July 12

Posted 03 December 2012 - 05:42 AM

So much useful and easily understood tutorial for newbie web developers or web designers to create appropriate upload file script. It clears all doubt of anyone who are lagging in it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1