File Upload in PHP :: Simplified
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:
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:
Enjoy!


Leave your response!