ron ron - 6 months ago 11
PHP Question

PHP Fail to upload image

test.php file

<?php

if(isset($_POST['submit']))
{
if(getimagesize($_FILES['image']['tmp_name'])==FALSE)
{
echo "Please select an image";
}
else
{
$image = addslashes($_FILES['image']['tmp_name']);
$image = file_get_contents($image);
$image = base64_encode($image);
saveimage($image);
}
}

function saveimage($image)
{
$con=mysql_connect("localhost","root","");

mysql_select_db("food",$con);

$query = "INSERT INTO info(image) VALUES ('$image')";
$result = mysql_query($query,$con);

if($result)
{
echo "<br> Image upload";
}
else
{
echo "<br> NOT";
}
}

function displayimage()
{
$con=mysql_connect("localhost","root","");

mysql_select_db("food",$con);

$query = "SELECT image from info";
$result = mysql_query($query,$con);
$row=mysql_fetch_array($result);
echo '<img src="data:image/jpeg;base64,'.$row["image"].'" width="200" height="200"/>';
}



?>


html file

<form method="post" action="test.php">

<label>Image:</label>
<input type="file" name="image"><br>

<input class="btn btn-primary" type="submit" name="submit" value="Confirm" style="height:50px; width:100px;">

<br><br>

</form>


Now i want to display the picture. But i keep getting a blank picture instead. Is there any problem with my display code there? Thanks

/dummy//aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa//dummy

Answer

add enctype="multipart/form-data" in your form

<form method="post" action="test.php" enctype="multipart/form-data">