I'm trying to insert an image into the database but when I click upload image, it gives the following error: Warning: getimagesize (): Filename can not be empty in /home/unn_w17015779/public_html/upload.php on line 9 File is not an image.;
HTML code:
<body>
<?php include 'addDataAdmin.php';?>
<form name="ContactForm" action="addDataAdmin.php" method="POST" enctype="multipart/form-data" autocomplete="off" onsubmit="return ValidateContactForm();">
ISBN:<input type="text" name="ISBN">
Author's Name:<input type="text" name="Authorsname">
Title:<input type="text" name="Title">
Edition:<input type="number" name="edition" >
Year:<input type="text" name="year" onkeypress="return justNumber(event)" >
Category:
<select name="category" size="1">
<option value="computing">Computing</option>
<option value="Romance">Romance</option>
<option value="Fiction">Fiction</option>
<option value="Non-Fiction">Non-Fiction</option>
</select>
<br />
Publisher:<input type="text" name="publisher">
Quantity-in-stock:<input type="number" name="quantityinstock" >
Price:<input type="text" name="price" onkeypress="return justNumber(event)">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit" formaction="/upload.php">
<input type="submit" value="Send" name="send">
<input type="reset" value="Clear">
</form>
PHP code:
<?php
include('config.php');
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 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["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if (!empty($_FILES["fileToUpload"]["name"])) {
$Image =$conn->real_escape_string($_POST['Image']);
$sql="INSERT INTO books (Image) VALUES('$Image')";
if(mysqli_query($conn,$sql))
{
echo '<h3><font color="red">You have successfully updated </font></h3>';
}
else
{
echo 'Error';
echo $sql;
}
}
?>;
What am I doing wrong? Thank you in advance.