Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

php - show image from blob mysql

Hi I have a image table in my database. These are stored as blob along with details such as image type & name.

I am having a problem showing the image, all I get is a white box with a red cross in it. code:

<?php

include '../connection.php';

$ID = $_GET['id'];

$query = "SELECT * FROM `images` WHERE `image_id` = '$ID'";

$result=mysql_query($query);
$row = mysql_fetch_array($result);

$image = $row['image'];
$image_type= $row['image_type'];   

header("Content-type: $image_type");
print $image; 

exit;

?>

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well here is a short answer.

<?php
include '../connection.php';
$id = (int)$_GET['id'];
$query = "SELECT * FROM `images` WHERE `image_id` = '$id'";

$result=mysql_query($query);
$row = mysql_fetch_array($result);

$image = $row['image'];
$image_type= $row['image_type'];
$size = $row['image_size'];
//alternative
/* list($image, $image_type, $size) = array(
                                       $row['image'],
                                       $row['image_type'],
                                       $row['image_size']
                                      );
*/
$ext = explode('/', $image_type);
$name = $id . '.' . $ext[1]; 

header("Content-type: $image_type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");

print $image;     
exit;

Check your blobtype to be a least MEDIUMBLOB which is able to store data up to 16M


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...