Jump to content

Display image from DB


SweNuckan

Recommended Posts

Hi,

 

Im having some problems with displaying images that are saved in my DB. Ive saved them as mediumblob, the code finds the correct image (ive checked it with outputting the filename) but it wont display it. Firefox tells me something like "The image cant be displayed cause theres something wrong with it" and IE only shows the HTML code and the images binary data.

 

Code:

<?php
$film = $_GET['filmNr'];						// Unique identifier
$getmovie = mysqli_query($connection, "SELECT * FROM Film WHERE filmNr='$film'");

if (!$getmovie) {
	$error = 'Ett fel inträffade under hämtningen av film: ' . mysqli_error($connection);
	include 'error.inc.php';
	exit();	}

while ($movie = mysqli_fetch_array($getmovie)) {
	$movies[] = "<p>Titel: " . $movie['titel'] .
				"<br />Pris: " . $movie['pris'] . " kronor <br /> Beskrivning: " . 
				$movie['beskrivning']; }

if (empty($movies)) {
	$error = 'Det finns inga sparade filmer. ';
	include 'error.inc.php';
	exit();	}

$getimage = mysqli_query($connection, "SELECT * FROM Bild WHERE filmNr='$film'");				

$image = mysqli_fetch_array($getimage);
$filnamn = $image['filnamn'];						// Filename
$filtyp = $image['filtyp'];							// FIletype
$fildata = $image['fildata'];						// Filedata
$disposition = 'inline';							

header("Content-type: $filtyp;");
header("Content-disposition: $disposition; filename=$filnamn");
header('Content-length: ' . strlen($fildata));

foreach ($movies as $movie) { ?>
		<p> <?php
			echo $movie; ?>
			<img src="<?php echo $fildata; ?>" />	
		</p>
<?php } 
?>

 

Some help would be appreciated ^^

Link to comment
https://forums.phpfreaks.com/topic/237422-display-image-from-db/
Share on other sites

If you're doing this for a class and need to save it as binary in a database, than ok. Otherwise, I highly suggest saving images just as paths in the database.

 

I'd suggest making another php file, let's say called image.php which servers the image. Your code will be much more tidy and you can reuse the code in several places.

 

image.php?id=10

<?php
$film = $_GET['id'];
$getimage = mysqli_query($connection, "SELECT * FROM Bild WHERE filmNr='$film'");				

$image = mysqli_fetch_array($getimage);
$filnamn = $image['filnamn'];
$filtyp = $image['filtyp'];
$fildata = $image['fildata'];
$disposition = 'inline';							

header("Content-type: $filtyp;");
header("Content-disposition: $disposition; filename=$filnamn");
header('Content-length: ' . strlen($fildata));

echo $fildata;
?>

 

using the image

<img src="image.php?id=10" />

 

You'll need to add error checking in your image.php file (if "id" isset and if "id" exists in the "Bild" table) and you're set. You can loop or whatever, just you'll need to pass the id to image.php.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.