By using PHP script and HTML form we can easily upload files to the server. We have to keep some rules in our mind while the uploading files to the server.

Set to on file_uploads directive in php.ini file.

Set an attribute of form tag method="POST" in form tag.

Set an attribute of form tag enctype="multipart/form-data",It identify which content type use while submitting form.

Set an attribute of input tag type="file", which shows the browse button at the user end.

You have to create one diretory where you want to upload the images.

Below is the example of Upload Image

This is home.php page

<!DOCTYPE html>
<form action="upload_image.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileName" id="fileName">
<input type="submit" value="Browse Image" name="submit">

This is upload_image.php page

$target = "upload_image/";
$target_file = $target . basename($_FILES["fileName"]["name"]);
$upload_or_not = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileName"]["tmp_name"]);  //Check image is actually iamge or not
if($check == false) {
$error_message = "File is not an image.";
$upload_or_not = 0;
}else if (file_exists($target_file)) {                    //Check image is exist or not on the server
$error_message = "Sorry, file already exists.";
$upload_or_not = 0;
}else if ($_FILES["fileName"]["size"] > 500000) {         //Check image size
$error_message = "Sorry, your file is too large.";
$upload_or_not = 0;
}else if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" //Check allowed image extension
&& $imageFileType != "gif" ) {
$error_message = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$upload_or_not = 0;
if ($upload_or_not == 0) {
echo "Sorry, your file was not uploaded due to this reason" .$error_message;
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileName"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileName"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";

Select image to upload:

Find us on

Facebook Google Plus Twitter Pinterest YouTube

web hit counter