File Upload in PHP
- Digital Engineering
File Upload in PHP
Though PHP file upload is very common practice in web development and it is actually very easy to do but still some newbies face problems while writing code to simply upload a file. This script here shows how to write a simple script for PHP file upload and applications of this script may be:
- Simple PHP File Upload
- Upload Image file via PHP
- Upload a File less than a certain Size
- Upload an Image of a certain resolution
There can be many more cases where we need PHP file upload but for now, we’re going to focus on solving above four needs which are very common.
So for uploading a file, first we need an HTML Form, from which the user will browse and select the file to be uploaded. Here is the code for the HTML file:
<html><body>
<form action="upload_file.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="Submit" /></form>
</body></html>
Now we’ve created the page, we’re going to write the code which will handle PHP file upload. Notice the form action is set to “upload_file.php” which contains the code for handling uploaded file. Now the code for upload_file.php is here :
<?php
if ($_FILES["file"]["error"] > 0) Â Â {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}  else  {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 />";
if (file_exists("upload/" . $_FILES["file"]["name"]))Â Â Â Â Â Â {Â Â Â Â Â Â echo $_FILES["file"]["name"] . " already exists. ";
}    else      {      move_uploaded_file($_FILES["file"]["tmp_name"],      "upload/" . $_FILES["file"]["name"]);      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}Â Â Â Â }
?>
Now this will simply upload the file to uploads folder. Notice how easy that was. Now lets proceed with some tricky parts. Suppose we need only specific type of files to be uploaded. For example, lets say we need to upload image files and we don’t want user to upload .txt or .pdf files or even more precisely we want only .jpg and .gif image files to be uploaded. Here is how we set the conditions for it:
if (($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")){
<code for PHPFile Upload>
} else {echo "invalid file type: Upload Failed.";}
Similarly we can add one more condition of file size:
if ($_FILES["file"]["size"] < 20000){<code for PHPFile Upload>} else {echo "File too large: Upload Failed.";}
Lastly suppose we need to upload an image of a particular resolution say 400×300, All other resolutions will be rejected. Here is the script to achieve this:
$attr = getimagesize($_FILES['logo']['tmp_name']);
$uploaded_size = $_FILES["logo"]["size"];
$uploaded_type = $_FILES["logo"]["type"];
//-----
if (($uploaded_type == "image/gif") || ($uploaded_type == "image/jpeg") || ($uploaded_type == "image/pjpeg"))
{
$ok=1;
}else {
$ok = 0;
$cause = "Invalid File Type.Only Jpeg/Gif image allowed.";
}
//-----
if(intval($attr[0])>280)
{
$ok = 0;
$cause = "Width cannot be more than 275px.";
}
//-----
if(intval($attr[1])>90)
{
$ok = 0;
$cause = "Height cannot be more than 90px.";
}
//-----
You must have got the idea by now how to upload and validate uploads in PHP. By tweaking these code snippets according to your needs, you’re going to be able to use them in your applications.
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s