Home » PHP

File Upload in PHP :: Simplified

7 December 2007 194 views No Comment Popularity: 2% Share/Bookmark

Here is the php code for file upload (image upload in this case) with description included within the code:

There are two files. ‘fileupload.html’ contains the form to upload file and ‘upload.php’ contains the php code.

Download link for the code and tutorial of file upload:

File Upload code

fileupload.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>File Upload</title>
</head>

<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
<p>File Upload</p>
<table>
<tr>
<td>File</td><td><input type="file" name="file" id="file" /> </td>
</tr>
<tr>
<td></td><td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</form>

</body>
</html>

upload.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>File Upload</title>
</head>

<body>
<?php
if(isset($_POST['submit']))
{
// first create a directory named 'uploads' inside your website directory
$uploadDirectory = "uploads";
// this is the directory name where you want to upload your files.
//this should be inside your website folder. suppose this file named upload.php is in
//folder named 'file upload' then the directory 'uploads' should be inside the directory 'file upload'

$name = $_FILES['file']['name']; // gives the file name (like logo.jpg)
$type = $_FILES['file']['type']; // gives the file type (image/jpeg)
$size = $_FILES['file']['size']; // gives the file size in bytes (like 16091)
$tmpName = $_FILES['file']['tmp_name']; // gives the temporary directory where the file is stored immediately after upload

//echo "$name , $type , $size , $tmpName ";

// suppose you want to show your file size in Kb (Kilobytes) then do the following
$sizeKb = ceil($_FILES['file']['size'] / 1024);
// the ceil function will round fractions up, ceil(4.3) = 5 and ceil(9.999) = 10

//echo "$sizeKb KB";

// suppose you want to allow only jpeg and gif image to be uploaded, and you also want the file size to be not more than 1MB
//i.e. your maximum file size limit is 1MB, then you need to do the following

// checking for file size and file type,
// for 1MB = (1024 * 1024 = 1048576)
// if you want to do for 500KB then (1024 * 500 = 51200)
if(($type != "image/jpeg" || $type != "image/gif") && ($size > 1048576))
{
echo "Only jpeg or gif image with size upto 1MB are allowed.";
}
else
{
// checking whether the file already exists in the upload folder
// $uploadDirectory and $name used here are defined above
if(file_exists("$uploadDirectory/$name"))
{
// if the file exits then print the message
echo "The file with the name $name already exists. Please choose another file and try again.";
}
else
{
// moving the file to uploads directory
move_uploaded_file("$tmpName","$uploadDirectory/$name");
// print the success message
echo "File has been successfully uploaded.";
}
}
}
?>
</body>
</html>

Download link:

File Upload code 

Enjoy!

From Mukesh Chapagain's Blog | Post File Upload in PHP :: Simplified

Related posts:

  1. Multiple file upload with jQuery and php
  2. Magento: How to upload file?
  3. File Upload in PEAR and Smarty
  4. Generating random image
  5. Regular Expression check, Validation in PHP

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.