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?

5
  • 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. Commented Mar 12, 2013 at 21:52
  • 1
    yes, store the images in your filesystem, the path to the image in a varchar in your database. Commented Mar 12, 2013 at 21:52
  • You cannot just echo the raw image contents to HTML. Commented Mar 12, 2013 at 21:53
  • Read this: stackoverflow.com/questions/5525830/… Commented Mar 12, 2013 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 Commented Mar 12, 2013 at 21:59

1 Answer 1

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'] ) . '" />';

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.