I've been stuck for someday trying to solve this problem. Still I doesn't find the clue.
I'm writing a simple blog with PHP and mysql. I'm uploading images files through form and storing them in a BLOB field:
This is the main fragment, I'm using PDO extension.
Code:
if ( $_FILES["archivo"]["error"] === 0 && $_FILES["archivo"]["size"] > 10 && $_FILES["archivo"]["size"] < 900000 && strpos($_FILES["archivo"]["type"], "image") !== false )
{
// opening temp file
$fh = fopen($_FILES["archivo"]["tmp_name"],"rb") or die ("Error no pudo abrirse el archivo temporal.<br />");
$imagen = addslashes(fread($fh,filesize($_FILES["archivo"]["tmp_name"])));
//$imagen = fread($fh,filesize($_FILES["archivo"]["tmp_name"]));
fclose($fh);
try
{
$stmt = $dbh->prepare('INSERT INTO sblog_post_pics SET blog_post_id=?, filename=?, filesize=?, imgdata=?, type=?');
$stmt->bindParam(1, $lastInId, PDO::PARAM_INT);
$stmt->bindParam(2, $_FILES["archivo"]["name"], PDO::PARAM_STR, 255);
$stmt->bindParam(3, $_FILES["archivo"]["size"], PDO::PARAM_INT);
$stmt->bindParam(4, $imagen, PDO::PARAM_LOB);
$stmt->bindParam(5, $_FILES["archivo"]["type"], PDO::PARAM_STR, 50);
$stmt->execute();
}
catch (Exception $e)
{
echo "Se ha producido un error al insertar la imagen <br />";
// echo "verbose: " .$e->getmessage() ."<br/>";
}
unset($imagen);
}
else
{
echo "Error: hubo algun problema con la imagen suministrada.<br />Recuerde que el archivo debe ser menor a 900 kb.<br />Los tipos de archivos permitidos son gif, jpeg y png.";
}
This is the table structure where image data is stored:
Code:
mysql> describe sblog_post_pics;
+--------------+--------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+-------------------+-------+
| blog_post_id | int(11) | NO | | 0 | |
| filename | varchar(255) | NO | | | |
| filesize | int(14) | NO | | 0 | |
| imgdata | mediumblob | NO | | NULL | |
| type | varchar(50) | NO | | | |
| timestmp | timestamp | NO | | CURRENT_TIMESTAMP | |
+--------------+--------------+------+-----+-------------------+-------+
imgdata is the blob field, and type is the mime-type, which I use later in header.
Code for storing the image seems to work ok. Put I can't get image shown. The code I used for this is the following:
displaypic.php
Code:
<?php
require '../../include/blg_conn_params.php';
$inId = $_GET["imgid"];
$dbh = new PDO('mysql:host=' .$blg_host .';dbname=' .$blg_bd, $blg_usuario, $blg_sena) or die ("No se pudo conectar a BD! de pics");
$stmt = $dbh->prepare('SELECT type, imgdata FROM sblog_post_pics WHERE blog_post_id=? ORDER BY blog_post_id DESC');
$stmt->bindParam(1,$inId, PDO::PARAM_INT);
$stmt->execute();
$stmt->bindColumn(1, $header_type, PDO::PARAM_STR);
$stmt->bindColumn(2, $image, PDO::PARAM_LOB);
$stmt->fetch(PDO::FETCH_BOUND);
header("Content-Type: ".$header_type);
echo $image;
// Debug
$fh = fopen("/tmp/testimg.png", "wb");
fwrite( $fh, stripslashes($image) );
fclose($fh);
$fh = fopen("/tmp/displaypic.log", "w");
fwrite($fh, $header_type."\n");
fwrite($fh, "image data size strlen=" .strlen($image) ."\n");
fclose($fh);
$stmt = null;
$dbh = null;
?>
Then to try to show the pic e call displaypic.php passing through GET the image's id:
Code:
<?php
$id="35";
?>
<html>
<head>
<title>Testing show pic</title>
</head>
<body>
<p>Just a test:</p>
<p><img src="displaypic.php?imgid=<?php print $id;?>" /></p>
</body>
</html>
I found within a lot of blogs, and code is very similar... But I can't get picture showed.
I was wondering if could be an specific apache issue... Something extra needed to be sent within http header? I don't know... Any suggestion will be very appreciated.
I'm using Debian (Lenny), PHP 5.3 and Apache 2 (apache2 - 2.2.9-10+lenny8 ).
Regards,
Matías