I'm trying to get an image file sent from html form in order to insert it in a database. It's unfortunately not working because the file is not posted.
All the other variables are working, I echoed them to check, but the file isn't. Where is my problem? I think it's at the html form, the inserted variables are also in the same form but in different divisions, so the post actually send all data in the form to post.php but not the image file.
As you can see I already include "enctype="multipart/form-data"".
Any help please?
1) HTML markup:
<form action="post.php" method="post" enctype="multipart/form-data">
<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000"/>
<div>
<input type="file" id="fileselect" name="fileselect[]" multiple accept="image/*"/>
<div id="filedrag">or drop files here</div>
</div>
<div id="submitbutton">
<button type="submit">Upload Files</button>
</div>
</form>
2) PHP code:
$title=isset($_POST['title'])?$_POST['title']:false;
$desc=isset($_POST['description'])?$_POST['description']:false;
$date=date("Y-M-D");
$city=isset($_POST['city'])?$_POST['city']:false;
$user=isset($_POST['name'])?$_POST['name']:false;
$price=isset($_POST['price'])?$_POST['price']:false;
$table=isset($_POST['option'])?$_POST['option']:false;
$imgfile=isset($_FILE['file']['name'])?$_FILE['fileselect']['name']:false;
$tmp_dir=isset($_FILE['fileselect']['tmp_name'])?$_FILE['fileselect'['tmp_name']:false;
$img_size=isset($_FILE['fileselect']['size'])?$_FILE['fileselect']['size']:false;
$upload_dir='images/'; // directory
$imgExt=strtolower(pathinfo($imgfile,PATHINFO_EXTENSION)); // GET IMAGE EXTENSION
$valid_ext=array('jpeg','jpg','png','gif');
//rename uploading image
$item_img=rand(1000,1000000).".".$imgExt;
// allow valid image format
if(in_array($imgExt, $valid_ext))
{
if($img_size < 500000)
{
move_uploaded_file($tmp_dir,$upload_dir.$item_img);
}
else{
$erMsg="Sorry , your file is too large";
}
}
else{
$erMsg="Sorry,only JPG,JPEG,PNG & GIF files are accepted :)";
}
$query = $pdo->prepare('INSERT INTO '.$table .'(title,description,image,date,city,user,price) VALUES(:title, :description, :image, :date, :city, :user, :price)');
$query->bindParam(':title', $title);
$query->bindParam(':description', $desc);
$query->bindParam(':image', $item_img);
$query->bindParam(':date', $date);
$query->bindParam(':city', $city);
$query->bindParam(':user', $user);
$query->bindParam(':price', $price);