gpt4 book ai didi

php - 如何使用 Laravel 5.* 将二进制数据插入 SQL

转载 作者:行者123 更新时间:2023-12-03 09:04:16 24 4
gpt4 key购买 nike

我想将图像保存到 MSSQL 中的 varbinary(max) 字段中。

我的 Controller 中的存储方法如下所示:

public function store(Request $request)
{

$vorname = $request->input('vorname');

$nachname = $request->input('nachname');

$file = $request->file('avatar');

$extension = $file->clientExtension();

$fullname = $nachname . "_" . $vorname . "." . $extension;

$file->storeAs('avatars' , $fullname);

$path = storage_path() . "\app\avatars\\". $fullname;

$imageData = unpack("H*", file_get_contents($path));

Mitarbeiter::create([

'PersonalNr' => request('personalnr'),
'Mitarbeiterschluessel' => request('mitarbeiterkey'),
'Vorname' => $vorname,
'Familienname' => $nachname,
'Bild' => $imageData[1]

]);

return view('welcome');

}

我收到错误:

SQLSTATE[42000]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Die implizite Konvertierung vom nvarchar(max)-Datentyp in varbinary(max) ist nicht zulässig.

翻译:

Implicit conversion from the nvarchar (max) data type to varbinary (max) is not allowed.

最佳答案

这段代码对我有用

if($request->input('Image')){
$datastring = file_get_contents($request->input('Image'));
$data = unpack("H*hex", $datastring);
$data = '0x'.$data['hex'];
$photo = \App\TraineeImage::where('ID', 13)->first();
$photo->update(['Image' => \DB::raw("CONVERT(VARBINARY(MAX), $data) ") ]);
}

关于php - 如何使用 Laravel 5.* 将二进制数据插入 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48261495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com