How to download/display bolb/bytea file from database use Laravel

We should use raw query instead of eloquent of laravel:

$uploadedFile = $this->uploadedFileRepository->findWithoutFail($id);

$f = \DB::table('uploaded_files')

->selectRaw('*')

->where('id', '=', $id)

->first();

$file_bytea = stream_get_contents($f->file_content);

if (function_exists('pg_unescape_bytea')) {

$file_str = pg_unescape_bytea($file_bytea);

} else {

$file_str = pack('H*', $file_bytea);

}

header("Content-type:".$f->mime_type);

//if (strpos($f->mime_type, 'image') === false && strpos($f->mime_type, 'pdf') === false) {

header("Content-Disposition: attachment; filename=".$f->file_name);

//}

echo$file_str;

exit();

Store bytea/blob file:

                if (function_exists('pg_escape_bytea')) {
                    $input[$i]['file_content'] = pg_escape_bytea(file_get_contents($files[$i]->getRealPath()));
                } else {
                    $input[$i]['file_content'] = bin2hex(file_get_contents($files[$i]->getRealPath()));
                }

Leave a Reply

Your email address will not be published. Required fields are marked *