1
I’m creating a digital menu that contains pictures of the dishes. I stored the photo in the Mysql BD using the following code:
HTML code
<form enctype="multipart/form-data" method="post" action="./processos/cadastra_item.php">
<p align="left">Imagem<br>
<input type="hidden" name="MAX_FILE_SIZE" value="99999999"/>
<input name="imagem" type="file"/>
</form>
PHP code
$imagem = $_FILES['imagem']['tmp_name'];
$tamanho = $_FILES['imagem']['size'];
if ( $imagem != "none" )
{
$fp = fopen($imagem, "rb");
$item->itemImg = fread($fp, $tamanho);
$item->itemImg = addslashes($item->itemImg);
fclose($fp);
}
After that I perform the INSERT normally. The field that stores the image is of type LONGBLOB. I checked by Workbench and the image was stored successfully but I can not display it on the page.
The code I used to try to display the image follows below:
echo "<center><img src='./processos/verImg.php?itemCod=\".$cod.\"' width='100' height='50' border='1'></center>"
PHP (File verImg.php)
$itemCod = $_GET['itemCod'];
//Encontra o item no banco utilizando o código
$sql = mysqli_query("SELECT itemCod, itemImg FROM Cardapio WHERE itemCod = ".$itemCod."");
$row = mysqli_fetch_object($sql, MYSQLI_ASSOC);
//itemImg é o campo onde esta armazenada a imagem
$bytes = $row->itemImg;
header( "Content-type: image/gif");
echo $bytes;
I also tried to use mysqli_fetch_array
but failed to display the image.
I forgot to mention that when I open the page, the place where the image should appear only appears an empty square. I tried with JPG, PNG and JPEG images.
– D_Alves
In this passage,
SELECT itemCod FROM
, where is the image field? It should not beSELECT itemImg FROM
?– Daniel Omine
@Danielomine really. This was an error when transposing the code into the Stack. I’ve corrected the question. The error is occurring with select done in the correct way :D
– D_Alves