Matheus Cirillo Matheus Cirillo - 1 year ago 60
MySQL Question

Failed to upload image to MySql databse using PHP

I'm trying to upload a picture into my MySql database using PHP, but the code reaches a else statement, seems like the image has a NULL value, but I've selected a file from my dir.

My HTML Form:

<form action="cadastrar.php" method="post">
Nome: <input type="text" name="nome" value=""><br>
Descrição: <input type="text" name="descricao" value=""><br>
Cor: <input type="text" name="cor" value=""><br>
Imagem: <input type="file" name="imagem"/><br>
<input type="submit" name="botao" value="CADASTRAR">

My PHP code:

if(isset($_POST['botao']) && isset($_FILES['imagem'])){
if(getimagesize($_FILES['imagem']['tmp_name']) == FALSE) //SEEMS LIKE THE CODE NEVER REACH THIS CONDITION..
echo "Please select an image.";
$imagem = addslashes($_FILES['imagem']['tmp_name']);
$imagem = file_get_contents($imagem);
$imagem = base64_encode($imagem);
$nome = $_POST["nome"];
$descricao = $_POST["descricao"];
$cor = $_POST["cor"];

$dP = new DaoProduto;
$p = new Pessoa;


echo "The code reach this ELSE..."; //<-- CODE REACHES HERE!!

Answer Source

You have to add the encryption in the form:

    <form action="cadastrar.php"  method="post" enctype="multipart/form-data" >
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download