php - How to upload ONLY images in database -


i want upload pictures , in database using php. 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 `feedbackzxc` values ('', '$username', '$product_image')");     echo "image uploaded!"; } else {     echo "only images allowed"; }  ?> 

but when upload file other images doesn't show error message. how can make show error message if file other image uploaded?

your else block message only images allowed shown must located after if block 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 allowed"; } 

Comments

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -