0

i want to download the image from one server, and want to save the binery data into mysql blob field. but its not working.

  1. if i am displaying the $picture1 then it shows the un-readable characters. byt $picture2 is not displaying.

  2. if i try to save it in the table, then its not saving in the table.

how can i save this into my blob field.

$picture1 = GetImageFromUrl($url);
$picture2 = addslashes(fread(fopen($picture, "r"), filesize($picture)));

print_r($picture1)
print_r($picture2)

function GetImageFromUrl($link)
{

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_POST, 0); 
curl_setopt($ch,CURLOPT_URL,$link); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$result=curl_exec($ch); 
curl_close($ch); 
return $result;
}

1 Answer 1

1

Try encoding it with base64 before saving in your database field:

echo base64_encode($picture1);

And to read out from database, you need to decode it:

echo base64_decode($picture);

But be sure to send the right headers with it, for example:

header('Content-Type: image/png');

But please read this before: Storing Images in DB - Yea or Nay? and consider if you really want to save it in your database.

I am strictly against it, but it's in your power

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

2 Comments

Also, be aware that the size of base64 encoded data is roughly 1.37 times bigger than the size of the original data.
its displaying the binary codes, but not saving it in the database.

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.