Reputation: 205
I have an image in my database saved as a large BLOB. and now I want to retrieve it and save it to my file system.
for this purpose I have done the following, but the images that were saved to my file system were corrupted ones.
Approach1:
$basedir = 'images/';
$imagename = strtolower(preg_replace('/([^\w\d\-_]+)/', '-', $row->name));
$filename = $basedir . $imagename . '_' . $row->id. '.jpg';
$file_content = base64_decode($row->image_data);
return file_put_contents($filename, $file_content);
Approach2:
$basedir = 'images/';
$imagename = strtolower(preg_replace('/([^\w\d\-_]+)/', '-', $row->name));
$filename = $basedir . $imagename . '_' . $row->id . '.jpg';
fopen($filename,'w');
if($fh = fopen("{$filename}", "wb")) {
fwrite($fh, base64_decode($row->image_data));
fclose($fh) ;
}
Please help!!!
Upvotes: 3
Views: 11225
Reputation: 787
This should do the trick if the BLOB isn't encoded:
file_put_contents('filename.jpg', $row->image_data);
Upvotes: 3