Page 1 of 1

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

#1 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Post icon  Posted 02 August 2009 - 06:32 AM

*
POPULAR

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.



STEP 1

Create a new HTML file and add the following code snippet:
<html>
<body>

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

</body>
</html>



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).


STEP 2

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:

<?php
// Check for any errors.
if ($_FILES["file"]["error"] > 0)
{
    print("Error: ". $_FILES["file"]["error"]);
}

else
{
    // 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.
    move_uploaded_file($_FILES["file"]["tmp_name"], 
    "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.



STEP 3

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:
_$FILES["file"]["type"]
_$FILES["file"]["size"]

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 />");

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



Is This A Good Question/Topic? 5
  • +

Replies To: PHP - Uploading a File

#2 javafreak  Icon 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  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 77
  • 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  Icon 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