0
I’m trying to insert an image into the database but when I click image upload, it gives the following error: Warning: getimagesize(): Filename cannot 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? Thanks in advance.
What error are you making? Edit the question and ask it too. It might be interesting to put the entire HTML form as well, because in PHP you are trying to save a value
$_POST["Image"]
which is not in the code shown.– Woss
is right initially I thought the error was in the query, but now I see that the problem is this, the variable $image is not in the form!
– Diana Madeira
I don’t know if that’s it. when I click upload image. appears this:Warning: getimagesize(): Filename cannot be Empty in /home/unn_w17015779/public_html/upload.php on line 9 File is not an image.;
– Diana Madeira
I know you want it in PHP, but take a look at this example, which also uses PHP:https://devdactic.com/ionic-image-upload-php/
– Ramos