Page 1 of 2

Help retrieving images from mysql

Posted: Tue Mar 23, 2010 10:34 pm
by guillermoer
I know is a little bit weird save images in a table on mysql; Now i'm able to save the images to the data base, but i cant get it into my html code.

I got the following code to upload the image

Code: Select all

 
<?php
   // file_upload.php
   // Check if the file uploaded successfully
   if(is_uploaded_file($_FILES["image_file"]['tmp_name']))
   {
      $image_type = "";
      //for security check the image type
      if( $_FILES["image_file"]["type"] == "image/jpeg" || $_FILES["image_file"]  ["type"] == "image/pjpeg" || $_FILES["image_file"]["type"] == "image/gif")
      {
         $image_type = $_FILES["image_file"]["type"];
         $tamano = $_FILES["image_file"]['size'];
         $image = addslashes (file_get_contents($_FILES['image_file']['tmp_name']));
 
         echo($_FILES["image_file"]["type"] . "<br>");
         echo("tamaño: " . $tamano ."<br>");
         // here you can save the image and image type in DB
        // You will output image type in header() when displaying image
 
        $connection = mysql_connect("localhost:3306","root","toor")or
    die ("Error en la conexión.");
 
    $db = mysql_select_db("web",$connection)
    or die ("Error en la selección de la bd.");
    
        $query = "INSERT INTO imagenes (imagen, tipo,tamano) VALUES('$image', '$image_type', '$tamano')";
        mysql_query($query) or die(mysql_error());
        echo "<br>Image id is ".mysql_insert_id();
        echo("<br><a href='prueba.php'>ver imagen</a><br>");
        echo("<a href='default.htm'>insertar</a><br>");
 
}
else
{
echo("Invalid image type");
}
}
 
?>
 
I got two pages, one is html with an img element that call a php page, this page get the image from the data base. Just doesnt work.

html page

Code: Select all

html>
<head>
<title>Image Test</title>
 
</head>
 
 
<body>
<h1>Displaying image from database</h1>
 
 <img src="verImagen.php?id=3" >
 
</body>
 
</html>
Php for image retrieving

Code: Select all

 
<?php
   error_reporting(E_ALL);
   ini_set('display_errors', '1');
   
    $link = mysql_connect("localhost", "root", "toor") or die("Could not connect: " . mysql_error());
 
    // select our database
    mysql_select_db("web",$link) or die("use ".mysql_error());
 
    //get image id
    $idImagen = $_GET['id'];
 
    // get the image from the db
    $sql = "SELECT * FROM imagenes WHERE idImagen=" . $idImagen;
 
 
    // the result of the query
    $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
    $arreglo = mysql_fetch_array($result);
     
     
    // set the header for the image
        //the type(tipo) and size(tamano) of the file are saved in the database
    header('Content-type: ' . $arreglo['tipo'] );
    header('Content-length: ' . $arreglo['tamano']);
 
   
    //print image
    echo $arreglo['imagen'];
    
    // close the db link
    mysql_close($link);
 
?>
 
In the page just appear a broken image. Someone can help me?

Re: Help retrieving images from mysql

Posted: Wed Mar 24, 2010 12:14 pm
by Sofw_Arch_Dev
Have you checked in the database to see that your image and all the columns are there? If so, do you know that the Content-type header is valid? What is the exact string output by:

Code: Select all

 
echo('Content-type: ' . $arreglo['tipo'] );
 
And to be complete, you may want to say this:

Code: Select all

 
header('Content-Transfer-Encoding: binary');
 

Re: Help retrieving images from mysql

Posted: Wed Mar 24, 2010 12:33 pm
by AbraCadaver
In addition to the previous question, what does this show:

Code: Select all

echo get_magic_quotes_runtime() . '<br/>' . get_magic_quotes_gpc();

Re: Help retrieving images from mysql

Posted: Wed Mar 24, 2010 1:47 pm
by guillermoer
to: Sofw_Arch_Dev

I use the Mysql Query Browser, the binary data and the descriptive data is there; Im able to preview the image. The table has five fields:
  • idImagen: is the table key, is auto increment
    imagen: is the image binary data, i use a mediumblob
    tipo: is the content-type, i use a varchar
    tamano: is the size, i use a varchar
I insert the contet type data in the upload code, the images have the following type.
image/pjpeg or image/gif.

the line

Code: Select all

echo('Content-type: ' . $arreglo['tipo'] );
print something like this:

Code: Select all

Content-type: image/gif
.

I will check the content-transfer-encoding.

Re: Help retrieving images from mysql

Posted: Wed Mar 24, 2010 2:01 pm
by guillermoer
I just add the

Code: Select all

header('Content-Transfer-Encoding: binary');
no change, Im usin php on IIS, do i need change any configuration on iis or in php.ini file?

Re: Help retrieving images from mysql

Posted: Wed Mar 24, 2010 2:22 pm
by Sofw_Arch_Dev
Neither image/pjpeg nor image/jpeg will work. "image/jpg" is what you want there.

Re: Help retrieving images from mysql

Posted: Wed Mar 24, 2010 7:40 pm
by guillermoer
change the content type to image/jpg; still have broken images.

Re: Help retrieving images from mysql

Posted: Wed Mar 24, 2010 7:47 pm
by Sofw_Arch_Dev
Did you hard-code a Content-type of image/jpg even for gif images or are you still using image/gif for GIFs? It's hard to diagnose without seeing code.

Re: Help retrieving images from mysql

Posted: Wed Mar 24, 2010 8:12 pm
by guillermoer
I hardcode the content type.

Code: Select all

<?php
   error_reporting(E_ALL);
   ini_set('display_errors', '1');
   
   
    $link = mysql_connect("localhost", "root", "toor") or die("Could not connect: " . mysql_error());
 
    // select our database
    mysql_select_db("web",$link) or die("use ".mysql_error());
 
    //get image id
    $idImagen = $_GET['id'];
 
    // get the image from the db
    $sql = "SELECT * FROM imagenes WHERE idImagen=" . $idImagen;
 
 
    // the result of the query
    $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
    $arreglo = mysql_fetch_array($result);
     
    //validaciones
    /*
    echo("tipo: " .$arreglo['tipo'] ." <br> tamaño: " . $arreglo['tamano']. "<br>");
    echo("<hr>");
    echo($arreglo['imagen']);
    */
       
    // set the header for the image
    
    [color=#BF0000]header('Content-type: image/jpg');[/color]
    header('Content-Transfer-Encoding: binary');
   
    //print image
     echo $arreglo['imagen'];
    
    // close the db link
    mysql_close($link);
 
?>

Re: Help retrieving images from mysql

Posted: Thu Mar 25, 2010 1:26 pm
by Sofw_Arch_Dev
So you hard-code the Content type to image/jpg. But what if you're image is gif? I'm trying to help you understand that you need to code conditions that support your DB.

On another note, it may be a good idea to echo the image data as the LAST thing the script does. Right now the last thing you do is close the db connection. If this line were to result in any output, you'd risk killing the output buffer and the image data.

Have you executed this code from the command line, not in a browser, and seen what the output looks like on screen? Can you validate that echoing the image data actually prints out image data?

Re: Help retrieving images from mysql

Posted: Thu Mar 25, 2010 3:21 pm
by guillermoer
You are right about the gif images, but, at the time i want the problem solve at leat with the jpg images; later i can update the code.

I move the close statement, so the image output is the las sentence.

I think the image is alright because i can preview them in the mysql query browser; I do not know how to check it in command line.

Re: Help retrieving images from mysql

Posted: Thu Mar 25, 2010 3:27 pm
by AbraCadaver
Since you ignored my first post, what happens if you set this manually and open verImagen.php in the browser (assuming that image 3 is a jpg):

Code: Select all

//get image id
//$idImagen = $_GET['id'];
$idImagen = 3;
Also, you're saving the image in a BLOB field?

Re: Help retrieving images from mysql

Posted: Thu Mar 25, 2010 8:12 pm
by guillermoer
deeply sorry, i miss it.

Well I try open the file verImagen.php hardcoding the parameter and sending it trough url verImagen.php?id=3; the results are:
  • If i put the instruction

    Code: Select all

    header(Content-type: image/jpg)
    , the page shows a broken image icon.
    If i remove the header instruction; the page shows a large amount of trash, i think is the binary data.
Im using a mediumblob type for the database field.

Re: Help retrieving images from mysql

Posted: Thu Mar 25, 2010 8:18 pm
by AbraCadaver
guillermoer wrote:deeply sorry, i miss it.

Well I try open the file verImagen.php hardcoding the parameter and sending it trough url verImagen.php?id=3; the results are:
  • If i put the instruction

    Code: Select all

    header(Content-type: image/jpg)
    , the page shows a broken image icon.
    If i remove the header instruction; the page shows a large amount of trash, i think is the binary data.
Im using a mediumblob type for the database field.
So do what I told you in my first post and I'll likely have your answer.

Re: Help retrieving images from mysql

Posted: Fri Mar 26, 2010 1:20 pm
by guillermoer
Both values are 0.

What does that mean :?: