PHP Upload Image In the below tutorial we will learn , How to Upload Image in PHP
Create a HTML form for uploading the image file.
- Method will be the post type i.e. method = βpostβ.
- Enctype will be used i.e. enctype = βmultipart/form-dataβ which specifies the content-type.
PHP Upload Image
1) Make a folder name as “images” for uploading.
2) File “index.php”
<html>
<head>
<title>Upload Image</title>
</head>
<body>
<form action="upload.php" enctype="multipart/form-data" method="post">
<b>Select the Image:</b>
<input type="file" name="image" id="image">
<input type="submit" value="Upload" name="submit">
</form>
</body>
</html>
Output
PHP getimagesize() Function
getimagesize() is the inbuilt function of PHP which is used for getting the image attributes i.e. its width, height, mime type etc.
array getimagesize( $file, $image_info )
$file (required): This argument refers to the image file name.
Return Value: This function return the array of image attributes i.e. width, height, mime type of image.
Example
<?php
//Put the image path. This is dummy image.
$imageInfo = getimagesize("developerhelps.jpg");
print_r($imageInfo);
?>
Output
Array
(
[0] => 500
[1] => 130
[2] => 3
[3] => width="500" height="130"
[bits] => 8
[mime] => image/jpg
)
Some restrictions in uploading file
Check Size of Image
// Check file size
if ($_FILES["image"]["size"] > 71680)
{
echo "File is too large.";
$error = true;
}
Check File Type
// Check file format
if($fileExtension != "png" && $fileExtension != "gif" && $fileExtension != "jpg" && $fileExtension != "jpeg" )
{
echo "PNG, GIF, JPG & JPEG files are allowed.";
$error = true;
}
3) File “upload.php“
<?php
$fileExtension = strtolower(pathinfo($_FILES["image"]["name"],PATHINFO_EXTENSION));
$error = false;
// Check if file is a actual image
if(isset($_POST["submit"]))
{
$imageAttribute = getimagesize($_FILES["image"]["tmp_name"]);
if($imageAttribute !== false)
{
echo "File Mime Type : " . $imageAttribute["mime"];
}
else
{
echo "This is not an Image file.";
$error = true;
}
}
// Check file size
if ($_FILES["image"]["size"] > 71680)
{
echo "File is too large.";
$error = true;
}
// Check file format
if($fileExtension != "png" && $fileExtension != "gif" && $fileExtension != "jpg" && $fileExtension != "jpeg" )
{
echo "PNG, GIF, JPG & JPEG files are allowed.";
$error = true;
}
if ($error)
{
echo "Something went wrong with file.";
}
else
{
$upload_dir = "images/";
//Remove the image override problem by below line
$upload_file = $upload_dir . pathinfo($_FILES["image"]["name"],PATHINFO_FILENAME) . "_" . time() . "." . $fileExtension;
if (move_uploaded_file($_FILES["image"]["tmp_name"], $upload_file))
{
echo "Image has been successfully uploaded.";
}
else
{
echo "Something went wrong with uploading.";
}
}
?>
Image file name as “developerhelps.jpg“
Output
File Mime Type : image/jpg
Image has been successfully uploaded.
More Related Post
- PHP MVC Framework
- Login Page in PHP
- PHP Array Functions Examples
- PHP Isset() vs Unset() Function
- PHP String Replace Function
- PHP array_key_exists() Function
Thanks for the reading post. I hope you like and understand the post. If you have any doubt regarding this post please comment below.