-1

I want to upload ONLY pictures , in the database using php. What I tried is,

<?php

if (isset($_POST['Upload'])) {
    $con = mysql_connect("localhost", "root", "");
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("iis", $con);
    $image = $_FILES["product_image"]["name"];
    $imageType = mysql_real_escape_string($_FILES["product_image"]["type"]);
    if (substr($imageType, 0, 5) == "image") {
        if (!file_exists("product_images")) {
            mkdir("product_images");
        }

        if ($_FILES["product_image"]["error"] > 0) {
            $error = "ERROR Return Code :" . $_FILES["product_image"]["error"] . "<br />";
        } else {
            move_uploaded_file($_FILES["product_image"]["tmp_name"], "product_images/" . $_FILES["product_image"]["name"]);
        }
    }

    $UserName = $_SESSION['id'];
    $product_image = ("product_images/" . $_FILES["product_image"]["name"]);
    mysql_query("INSERT INTO `feedbackzxc` VALUES ('', '$UserName', '$product_image')");
    echo "Image Uploaded!";
} else {
    echo "Only images are allowed";
}

?>

But when I upload a file other than images it doesn't show the error message. How can I make it show error message if a file other than an image is uploaded?

showdev
  • 28,454
  • 37
  • 55
  • 73
satish
  • 41
  • 4
  • 1
    possible duplicate of [uploaded file type check by PHP](http://stackoverflow.com/questions/6755192/uploaded-file-type-check-by-php) – JasonCG Dec 05 '14 at 21:25

1 Answers1

1

Your else block where the message Only images are allowed is shown must be located after the if block that check this: substr($imageType,0,5) == "image"

if(substr($imageType,0,5) == "image"){
    if(!file_exists("product_images"))
    {
        mkdir("product_images");
    }
    if($_FILES["product_image"]["error"] > 0)
    {
        $error = "ERROR Return Code :" . $_FILES["product_image"]["error"] . "<br />";
    }
    else
    { 

        move_uploaded_file($_FILES["product_image"]["tmp_name"], "product_images/". 
        $_FILES["product_image"]["name"]);  
    }
}
else
{
    echo "Only images are allowed";
}
Verhaeren
  • 1,661
  • 9
  • 10