Skip to main content
Upload Image with HTML PHP

How to upload Images With HTML and PHP

First of all, you need to know that your local server is running which is Xampp, Wampp for Windows and MAMPP for MAC.

Make a new folder under htdocs in xampp and in Wampp under www directory.

For example, let’s say I have to make a folder exampleone

Now inside exampleone make new file lets say contact.html or if you want to store the image path in the database and the image in a folder so you need to change the extension to contact.php instead of contact.html.

Now inside conact.php

Below is the code

 


<!DOCTYPE html>
<html>
<body>

<form action="uploadImage.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

Make Sure that your Form tag contains the method “POST” and enctype=”multipart/form-data”.

Now head Over to uploadImage.php file.

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}

This will do the image uplaoding to the folder if you are interested to store in the database the image name let me know in comments below.

Thanks.

 

One thought to “How to upload Images With HTML and PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *