1

I have a database where images are saved as blob. Now, I want to convert this blob data into image files.

So I have made this code, but its not giving any result

$sql_icon = "SELECT $off_table.icon,$off_table.id
            FROM $off_table,$cnt_table,$ctg_table
            WHERE  $off_table.id = $cnt_table.id
            AND $off_table.id = $ctg_table.id
            AND ".$cond_api."
            AND ".$cond_nt."
            AND ".$cond_cnt."
            AND ".$cond_ctg;


$result_icon = mysql_query($sql_icon);

while($row = mysql_fetch_array($result_icon)) 
    {
       $image = $row["icon"];
       $id = $row["id"];

       $file = fopen("./image/".$id.".jpg","w");
       fwrite($file, base64_decode($image));
       fclose($file);
    }

The issue is, I am getting files but no extensions and if I rename them to ".jpg" extension, then its displaying "no preview available"

NOTE:

The images are saved into database by this method

$sql = "INSERT INTO aw_offers_v2
                    (
                    id,name,description,payout_type,
                    payout,expiration_date,creation_date,preview_url,
                    tracking_url,categories,countries,countries_short,
                    api_key,network_id, icon,icon_size)
                    VALUES
                    ('$id','$name','$description','$payout_type',
                    '$payout','$expiration_time','$creation_time','$preview_url',
                    '$tracking_url','$categories','$countries','$countries_short',
                    '$api','$api_url','".mysql_real_escape_string(file_get_contents($cover_image_src))."','".strlen(file_get_contents($cover_image_src))."')";

If I write this code

echo '<img src="data:image/jpeg;base64,'. base64_encode($image).'" />';

then I can see the images properly. That means the images are successfully saved.

But I can't store them in folder as files.

halfer
  • 19,824
  • 17
  • 99
  • 186
Saswat
  • 12,320
  • 16
  • 77
  • 156

1 Answers1

1

By looking at your code, you don't base64_encode your image.

So you can just do:

 fwrite($file, $image);

You only need base64_encode when you display the image putting the data inside the src attribute of <img tag.

Also note that you can simplify this:

$file = fopen(...);
fwrite($file, $image);
fclose($file);

with a single line:

file_put_contents("./image/{$id}.jpg", $image);
dynamic
  • 46,985
  • 55
  • 154
  • 231
  • This is correctemundo. I would also higly recommend you to have a chat with your client to discuss how to store the images. A dabase is not the right tool for this. – Jørgen R Nov 28 '14 at 13:58
  • Guys, let me clear it.... My client saves the images in the blob. Then in a panel he selects some conditions and click on a button to save the data in csv and get a zip file which contains the images files. Thats why i need to save the mages from blob to a file and then create a zip out of it. Thats all the reasons i am putting it like this way. He just want to have it this way.... I told him to save the path in db, but he wants it like this – Saswat Nov 28 '14 at 14:26
  • It's safe in this case, because `$id` comes from the database, but it's worth pointing out that this usually needs untainting. If this value could come from the user, that write operation could be unintentionally destructive - all you need are a few `..` directory traversals, and you can write anywhere the web server has write permissions. – halfer Aug 07 '15 at 11:33