2

I am a newb with 6 months experience, self-taught via StackEx/books/etc. Created a pretty decent website with login/register and storing some info via mySQL. I have been through every single BLOB post here and I have some decent output.

I think I, like most newbs, know enough to be dangerous, don't have the greatest foundation laid out so when it gets to serious understanding of built-in functions, arrays and passing arguments we can lose the flow and I basically think I have dug a hole by using includes to call some navbar so that I can't just use a header to output the damn image as I echo the user name after login so it has already outputted lines and it will be a monster to undo. Three levels of nav, unauthenticated, authenticated and admin.

The database connection and write to/read from is OK. I can store the BLOB and I can even read it back and store the array in a variable and then debug see the binary but I can't get it to display on an HTML page.

Here is the fun:

$stmt = $dbc2->query("SELECT * FROM equip1");

while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$name = $row['equipname1'];
$desc = $row['equipdesc1'];
$img = $row['equipimg1'];
}

<div class="col-lg-12">
    <h1><?php echo $name ?></h1>
    <p><?php echo $desc ?></p>
    <pre>
        <?php print_r($img); ?>
    </pre>
    <?php echo "<img src='data:image/jpeg;base64," . base64_encode( $img ) . "' />"; ?>
</div>

$dbc2 connects equip1 table is two simple varchar cols, one BLOB col. $name and $desc echo out ok, the $img displays broken link. I check the array via pre code and is matching, per what is stored in dB during upload. From what I have read and gone through, seems like you can't do this at same time unless via data URI which I have done but still broken link. Not making sense to me at all. I try the header and of course output already started but I can see also outputs the binary.

Here is screenshot of the URI method:

binary

Since both methods "seem" to get into and out of the dB but do not display I'm going round in circles. Please help me out to display the image on an html page. I would like to echo it anywhere and then I can just style the page after that. Thanks!

1
  • I've really gone at this for 6-8 hrs groan and I promise I will study harder and pray to the PHP gods to ye who gets the answer. :) Commented Feb 23, 2016 at 19:02

2 Answers 2

3

Try Like this

<?php echo '<img src="data:image/png;base64,'.base64_encode($img).'">';?>
Sign up to request clarification or add additional context in comments.

5 Comments

No change. It is a jpeg as well. Thanks for your answer! :)
@KevinMorris no see the single quote and Double quote place did u checked this one??
Yes Pranavadurai! But you helped! You rebooted my brain a little so I noticed I was double-encoding. I encoded the base64 when I stored it. Thanks!!!! :)
Also want to note that Prana format was more "correct". I was using single quotes to echo which I normally do but during all the debugging I swapped out the line thinking it was something to do with the concatenate of the html and php variable. Using Prana line above is clean, just substitute your variable for $img.
First i thought you have problem because of this when i new to php i done mistake in this that's why i said to check this anyway happy coding :)
0

Uhh NEVER MIND. Never thought reading someones answer would make for "fresheyes". I noticed I was encoding but I already encoded it when I stored it. Mea culpa. DOH! :)

5 Comments

I was about to remark that the output data you have rendered didn't look like a JPEG header! :-)
Yeah well...bit off more than I could chew I guess. Noticed the edits, thanks. Added all that fluff as I don;t post unless I have already gone round the bend and back so to speak and some dudes tend to smack you around thinking ur just being lazy. Wow I can actually breathe again...cool!
Also THANKS @Pranavadurai! Not sure If i was explicit.
No worries about the edits. I favour detailed, well researched and succinct - a lot of fluff and/or meta discussion can, paradoxically, encourage a negative response.
No worries about the edits. I favour detailed, well researched and succinct - a lot of fluff and/or meta discussion can, paradoxically, encourage a negative response.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.