I wrote code below using PHP, mySQL and AJAX to form submission.
When the user wants to upload an image larger than 700*400, the alert message such as "the image size doesn't match" doesn’t show up. Why?
What I have tried:
JQuery Code
$("#addproduct").submit(function(event){
event.preventDefault();
$.ajax({
url: "admin.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data){
var num=data.search("invalid_file");
if(num!=-1)
alert("the image size doesn't match...");
}
});
});
PHP Code
if($_SERVER['REQUEST_METHOD']=="POST" && isset($_POST['tahahiddenadd'])){
$pName=$_POST['tahaproductname'];
$pPrice=(int)$_POST['tahaproductprice'];
$pDesc=$_POST['tahaproductdesc'];
$pPath="image/site/noProductPic.jpg";
if(isset($_FILES['tahaproductpicpath'])){
if($_FILES['tahaproductpicpath']['error']==0 && ($_FILES['tahaproductpicpath']['type']=="image/jpeg" || $_FILES['tahaproductpicpath']['type']=="image/gif" || $_FILES['tahaproductpicpath']['type']=="image/png")){
$imageSize=getimagesize($_FILES['tahaproductpicpath']['tmp_name']);
$width=$imageSize[0];
$height=$imageSize[1];
if($width>700 || $height>400){
echo "invalid_file";
exit;
}
else{
move_uploaded_file($_FILES['tahaproductpicpath']['tmp_name'],'image/product/'.$_FILES['tahaproductpicpath']['name']);
$pPath="image/product/".$_FILES['tahaproductpicpath']['name'];
}
}
}
$con=mysqli_connect("localhost","root","");
if(!$con)
die("");
else{
mysqli_select_db($con,"tahaDB");
mysqli_query($con,"insert into tahaproducttable(productName,productPrice,productDesc,productPicPath) values('$pName',$pPrice,'$pDesc','$pPath')");
mysqli_close($con);
}
}
HTML From
<form id="addproduct" enctype="multipart/form-data" style="direction:rtl">
<input type="text" name="tahaproductname" id="tahaproductname" maxlength="50" />
<input type="text" name="tahaproductprice" id="tahaproductprice" maxlength="10" />
<textarea name="tahaproductdesc" id="tahaproductdesc" maxlength="300"></textarea>
<input type="file" name="tahaproductpicpath" id="tahaproductpicpath" accept="image/jpeg, image/gif, image/png" />
<input type="hidden" name="tahahiddenadd" id="tahahiddenadd" value="tahahiddenadd" />
<input type="submit" name="tahasubmitadd" id="tahasubmitadd" value="Add Product" />
</form>