0

Found several examples online on how to upload an image to a mysql database table, in binary NOT a link or a folder in the server.

the imag isn't viewable when i try to print it

when I check the database table,it shows a bunch of data in weird format, i'm assuming the image data

here is code

if(!empty($_FILES['image']) && $_FILES['image']['size'] > 0 && !empty($_POST['name']))
{
    // Temporary file name stored on the server
       $tmpName = $_FILES['image']['tmp_name'];

// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);

code for displaying image

$sql = "SELECT * FROM `photos` WHERE userName = '$currentUser'";
    $result = mysqli_query($con,$sql);
    while($row = mysqli_fetch_array($result)) 
    {
        $content = $row['image'];
        echo $content;
        echo '<p id="caption">'.$row['caption'].' </p>';
    }

when i try to display the image i get nothing as output, i check the database via putty, and i see a massive amount of weird characters, i'm assuming the items for the image.

ideas?

user1050632
  • 680
  • 4
  • 13
  • 26
  • 1
    `addslashes` is already not-so-good. MySQLi supports parametrized queries — use them. Also, you can’t just output the bytes of an image into an HTML page and expect it to show up as an image. – Ry- Mar 12 '13 at 21:52
  • 1
    yes, store the images in your filesystem, the path to the image in a varchar in your database. – michi Mar 12 '13 at 21:52
  • You cannot just echo the raw image contents to HTML. – Green Black Mar 12 '13 at 21:53
  • Read this: http://stackoverflow.com/questions/5525830/displaying-an-image-stored-in-a-mysql-blob – Dragos Mar 12 '13 at 21:55
  • Dragos i read that article and its working better, shows a little square, however there is no image still. will it only work with jpeg? how do i do it so taht more than 1 type of image is viewable – user1050632 Mar 12 '13 at 21:59

1 Answers1

2

you could eventually try to replace these two lines:
$content = $row['image']; echo $content;
with:
echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['image'] ) . '" />';

Dragos
  • 1,824
  • 16
  • 19
  • Dragos i read that article and its working better, shows a little square, however there is no image still. will it only work with jpeg? how do i do it so taht more than 1 type of image is viewable – user1050632 Mar 12 '13 at 22:00