0

The database I am using is Postgresql and I downloading a webpage and storing in a BLOB. Like this:

<?php
$html = file_get_contents('http://www.example.com');
$encoded_html = base64_encode($html);

//Store encoded data in blob in database
?>

That part works fine. But when I try to decode it and display it, it comes out garabled.

<?php echo base64_decode($encoded_html); ?>

Do I have to add extra parameters when encoding and decoding the data?

4
  • 1
    why you want to encode? it takes 33% more memory! Commented Mar 3, 2013 at 22:41
  • Besides that if you are base64 encoding then you don't need a blob, just plain text. Commented Mar 3, 2013 at 22:43
  • Indeed, take care of the place used by base64. it is greedy of memory. You can add directly your content, escaping your string to prevent error from your postGreSQL. That will be fine and more simple. Commented Mar 3, 2013 at 22:43
  • were you able to resolve the problem? Commented Mar 4, 2013 at 17:06

1 Answer 1

1

If this works then the encode/decode functions work fine

<?php
$html = file_get_contents('http://www.example.com');
$md5 = md5($html);
$encoded_html = base64_encode($html);
$decoded_html = base64_decode($encoded_html);
echo (md5($decoded_html) == $md5) ? 'OK' : 'FAIL';
echo PHP_EOL;

If not then I'd suggest comparing the base64 data that you're putting into the database with what comes out.

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

Comments

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.