3
我的數據庫中有一個圖像保存爲大BLOB。現在我想檢索它並將其保存到我的文件系統。將blob保存到php中的圖像文件
爲此我做了以下操作,但保存到我的文件系統的圖像已損壞。
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) ;
}
請幫助!
在BLOB數據爲base64編碼? BLOB的意義在於存儲二進制數據,而base64的意義在於將二進制數據轉換爲純文本,因此可以將其存儲在VARCHAR中。 – DaveRandom
你有祕密圖片嗎? – Oyeme
是的base64將利用更多的資源(內存,處理)。 –