Today I am going to teach you how to upload photos using Html and Php.
A lot of website like Facebook, Twiitter and Google+ allow their users to upload the images on their profile, so how it’s work?
I will teach you basic of how to upload images using Html and Php with little validation.
Do First Step By Step:
- First create folder in www directory, name this folder as upload.
- Now open your upload folder.
- Create new folder in upload, name this folder as uploaded_images.
- This folder “uploaded_images” will save our uploaded images.
Let’s code Html form first
<form action="" method="post" enctype="multipart/form-data">
<center> <table>
<h1>How To Upload Image</h1>
<tr><td>Select Image</td><td><input type="file" name="image" id=""/></td></tr>
<tr><td><input type="submit" value="Upload Image" /></td></tr> </table>
<h1>Author Abuzar Ansari</h1>
</center>
</form>
Let’s code Html form first
<center> <table>
<h1>How To Upload Image</h1>
<tr><td>Select Image</td><td><input type="file" name="image" id=""/></td></tr>
<tr><td><input type="submit" value="Upload Image" /></td></tr> </table>
<h1>Author Abuzar Ansari</h1>
</center>
</form>
It is important to use
enctype=”multipart/form-data ” for uploading file purpose if we not use
it, our uploading script will not upload images.
Make sure your input type is set to file.
Set the name image.
Let’s write php script for uploading image.
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$image_name = $_FILES['image']['name'];
$image_size = $_FILES['image']['size'];
$image_type = $_FILES['image']['type'];
$image_tmp = $_FILES['image']['tmp_name'];
if($image_type == 'image/jpeg' or $image_type == 'image/png' or $image_type == 'image/gif' )
{
$uploaded = move_uploaded_file($image_tmp, "images/$image_name");
if($uploaded)
{
echo '<script type="text/javascript">alert("Image Has Been Successfuly Uploaded");</script>';
}
}
else
{
echo '<script type="text/javascript">alert("Please Upload Only Jpeg, Png Or Gif Format File ");</script>';
}
}
?>
<!--- abuzaransariweb@gmail.com Author Abuzar Ansari --->
PHP Script Explanation:
We have used If condition that mean if request is post then
it will execute if statements.
We are using POST method.
Uploading data is multipart data like image name, image
size, image type, image temp it’s a multiple data.
We have created local variables like $image_name,
$image_size etc.
All the local variables holding the value of Image name,
size etc.
We used if condition to check it is image format file or
another format file.
If it is a image format file like Jpeg, Png, Gif then code
will execute, if it is another format file ‘else’ statement code will execute.
If it is a image format file then our if statement will
execute and upload our file sucessfuly.
We use move_uploaded_file() function for uploading purpose.
Now go to your images directory, you have successfully uploaded
images.
We can use many validation like if image size is larger than
1mb or if image is not selected, this is a basic example for uploading images.
Thank you. If you face any problem just comment below.
Thanks Sanjay!
ReplyDeletenice profile
ReplyDeletesorry tutorial
ReplyDeleteThank You!
DeleteVery Excellent Tutorial dear .your great
ReplyDelete