SweNuckan Posted May 25, 2011 Share Posted May 25, 2011 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 ^^ Quote Link to comment https://forums.phpfreaks.com/topic/237422-display-image-from-db/ Share on other sites More sharing options...
Fadion Posted May 25, 2011 Share Posted May 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237422-display-image-from-db/#findComment-1219971 Share on other sites More sharing options...
SweNuckan Posted May 25, 2011 Author Share Posted May 25, 2011 Actually there aint any specific reason. I just followed the instructions from a book, im doing this as a part of a course project. Quote Link to comment https://forums.phpfreaks.com/topic/237422-display-image-from-db/#findComment-1219975 Share on other sites More sharing options...
Fadion Posted May 25, 2011 Share Posted May 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237422-display-image-from-db/#findComment-1220081 Share on other sites More sharing options...
SweNuckan Posted May 26, 2011 Author Share Posted May 26, 2011 I decided to use the other method instead. I got it working and it was much easier then my first approach. Thanks for your time and the help, really appreciated. =) Quote Link to comment https://forums.phpfreaks.com/topic/237422-display-image-from-db/#findComment-1220548 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.