我正在尝试使用laravel将一些上传图片存储到我的数据库中。一切顺利,一切都存储了,但是对于文件,他们一直存储38B的bin文件,我尝试将其读取为.Txt文件,并且它具有/ Applications / MAMP / tmp / php / phpUzMXbn的路径。 这是我的功能代码:
Route::post('/FruitCreate',function(Request $request){
$fruit = new fruit;
$fruit->name = $request->name;
$fruit->price = $request->price;
$fruit->picture = $request->image;
$fruit->save();
return redirect('FruitsChangingPricePanel');
我的表单刀片:
<form enctype="multipart/form-data" method="POST" action="{{ url('FruitCreate') }}" >
{{ csrf_field() }}
<input type="text" name='name'>
<input type="text" name='price'>
<input type="hidden" name="MAX_FILE_SIZE" value="30000000" />
<input type="file" name='image'>
<button type='submit'> submit </button>
感谢您的帮助!
答案 0 :(得分:0)
您可以这样做:
$file = $request->file('image');
$imageContent = $file->openFile()->fread($file->getSize())
;
$fruit = new fruit; $fruit>picture = $imageContent; $fruit>save();
注意:您的列类型必须为Blob
答案 1 :(得分:0)
因为您正尝试直接保存bin。 试试这个
$file = Input::file('file');
$destinationPath = public_path(). '/uploads/';
$filename = $file->getClientOriginalName();
$file->move($destinationPath, $filename);
echo $filename;
//echo '<img src="uploads/'. $filename . '"/>';
$user = ImageTest::create([
'filename' => $filename,
]);
答案 2 :(得分:0)
首先,应该获取图像,然后将其存储到public/uploads/fruits
文件夹中。之后,将图片的路径保存到DB。
$fruit = new fruit;
$fruit->name = $request->name;
$fruit->price = $request->price;
if ($request->has('image')) {
if (!file_exists(public_path('uploads/fruits/'))) {
mkdir(public_path('uploads/fruits/'));
}
if (!file_exists(public_path('uploads/fruits/' . date('FY') . '/'))) {
mkdir(public_path('uploads/fruits/' . date('FY') . '/'));
}
$image = $request->file('image');
$filename = public_path('uploads').'/fruits/' . date('FY') . '/' . str_random() . '.' . $image->guessExtension();
\Image::make($image->getRealPath())->encode('jpg')->resize(220, 220)->put($filename);
$fruit->picture = $filename;
}
$fruit->save();