将blob保存到php中的图像文件中

时间:2022-10-07 01:23:22

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.

在我的数据库中,我有一个保存为一个大BLOB的图像。现在我想要检索它并保存到我的文件系统中。

for this purpose I have done the following, but the images that were saved to my file system were corrupted ones.

为此,我做了以下工作,但是保存到我的文件系统的映像是损坏的。

Approach1:

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:

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!!!

请帮助! ! !

1 个解决方案

#1


3  

This should do the trick if the BLOB isn't encoded:

如果BLOB没有编码,那么这应该可以实现:

file_put_contents('filename.jpg', $row->image_data);

#1


3  

This should do the trick if the BLOB isn't encoded:

如果BLOB没有编码,那么这应该可以实现:

file_put_contents('filename.jpg', $row->image_data);