Hi, for some reason, i want to upload an image to a mysql db, i know that 99% of the people that knows say it is a bad idea, still wanna give it a try, first i used this code:
function almacenaImagen($IMG,$ID)
{
$this->image=$IMG;
ob_start();
imagejpeg($this->image);
$jpg = ob_get_contents();
ob_end_clean();
$jpg = str_replace('##','\#\#',mysql_escape_string($jpg));
$act="UPDATE oficios SET Imagen='$jpg'WHERE ID_OFICIO='$ID'";
mysql_query($act,$this->conexion);
}
/*para recuperar la imagen guardada en el tipo blob*/
function recuperaImagen($ID)
{
//$this->ID=$ID_OFICIO;
$consulta = "SELECT IMAGEN FROM OFICIOS WHERE ID_OFICIO='$ID'";
$result=mysql_query($consulta,$this->conexion);
$result_array = mysql_fetch_array($result);
//header("Content-Type:image/jpeg");
echo $result_array[0];
}
You probably wonder WHY UPDATE? well, it's 'cause i store some other info in the table that holds the image, then i update it, with this code:
class Oficio
{ /*funcion sin sentido que hace que todo funcione magico*/
function setConexion($link)
{
$this->conexion=$link;
}
/*funcion que rellena los valores de en la tabla de oficio*/
function setOficio($ID,$NOM,$ASUNTO,$DEST,$REM,$IMG)
{
$consulta="INSERT INTO oficios
(ID_OFICIO,NOMBRE_OFICIO,ASUNTO,DESTINATARIO,REMITENTE)
VALUES('$ID','$NOM','$ASUNTO','$DEST','$REM')";
mysql_query($consulta,$this->conexion);
$IMAGEN=NEW imagenes;
$IMAGEN->setConexion($this->conexion);
$IMAGEN->almacenaImagen($IMG,$ID);
}
}
It works aparently (i get values on the db)
The problem comes when i try to retrieve the image, to do that i just call this function:
function recuperaImagen($ID)
{
//$this->ID=$ID_OFICIO;
$consulta = "SELECT IMAGEN FROM OFICIOS WHERE ID_OFICIO='$ID'";
$result=mysql_query($consulta,$this->conexion);
$result_array = mysql_fetch_array($result);
//header("Content-Type:image/jpeg");
echo $result_array[0];
}
The form where the user selects the image targets to a script called "handler" which calls oficio, i'm only begining, and i know i'm coding in an awkward way 😛
And i don't know much of php (as you can see), but when i try to retrieve the image (via ID_OFICIO) i get this:
Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\xampp\htdocs\Cooperacion\imagenes.php on line 30
When the function associated to this warning is not even called, and line 30 would be:
ob_start();
imagejpeg($this->image); <--------- line 30
$jpg = ob_get_contents();
I also tried a tutorial that i found here, but that doesnt work at all.