skip to Main Content

what I need to do in my website is to show a list of jpg images stored in a Mysql database using Blob type (I don’t want to use any other way). but instead of the image I got strings like this

����$ExifMM*bj(1r2��i��SI'SI'Adobe Photoshop CS6 (Windows)2014:07:31 20:02:56�����&(."�HH����XICC_PROFILEHLinomntrRGB XYZ � 1acspMSFTIEC sRGB���-HP cprtP3desc�lwtpt�bkptrXYZgXYZ,bXYZ@dmndTpdmdd��vuedL�view�$lumi�meas$tech0rTRC<�gTRC<�bTRC<�textCopyright (c) 1998 Hewlett-Packard CompanydescsRGB IEC61966-2.1sRGB IEC61966-2.1XYZ �Q�XYZ XYZ o�8��XYZ b����XYZ $����descIEC http://www.iec.chIEC http://www.iec.chdesc.IEC 61966-2.1 Default RGB colour space - sRGB.IEC 61966-2.1 Default RGB colour space - sRGBdesc,Reference Viewing Condition in IEC61966-2.1,Reference Viewing Condition in IEC61966-2.1view��_.����XYZ L.....

This is the code…

<?php

// Create connection
$con=mysqli_connect("erossenses@localhost","erossenses","-----","my_erossenses");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM Racconti");

echo "<br>";

echo "<font style="font-family:Verdana; font-size:15px; color:maroon"><b><u>Titolo</u></b></font>";
echo "<br>";
echo "<br>";



header('Content-type: image/jpeg');

while($row = mysqli_fetch_array($result)) {

   echo $row['Immagine'];

   echo "<br>";
}

mysqli_close($con);
?>

Where am I wrong?

3

Answers


  1. You can’t generate multiple images with php at once. you can only do them one at a time, so you’d have to do it via ajax. this isn’t advisable at all anyway. you’re best to learn about ajax and file uploads and how to secure them than wasting any more time on this. Storing images in mysql is suicide, especially for a beginner. You are on the wrong path and learning dangerous habits already.

    to advance your skills, read the manual http://php.net/ or better yet, get a quality php book.

    Login or Signup to reply.
  2. The problem is you’re echoing a lot of stuff that’s not an image. If you set header('Content-type: image/jpeg'); the browser will expect to get only the image data (for just one image).

    If you really must use images from the database, a solution would be to create an additional file, let’s say image.php?id=1 which gets the id of the image and displays only that specific image. You can use this file in the src of an img tag.

    Login or Signup to reply.
  3. Try the below code

    header(“Content-length: $fileSize”); header(“Content-type: $fileType”); header(“Content-disposition: download; filename=$fileName”); echo $fileData;

    Hope file size,name, type, and data stored in table.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search