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
Share on other sites

Is there any reason why you're saving images as binary data? It's not something of a great solution in most cases, because you can save in database just the image path and keep that image in a folder. The system will be easier to mange too.

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.