Laravel 图片上传到数据库
Laravel image upload to database
这个函数是我用来存储新公司的函数:
public function store(Request $request)
{
$file = $request->file('logo');
$filename = 'company-logo-' . time() . '.' . $file->getClientOriginalExtension();
$path = $file->storeAs('public', $filename);
dd($path);
Company::create([
'name' => $request->name,
'email' => $request->email,
'logo' => $request->logo,
'website' => $request->website
]);
return redirect('/company/all');
}
此视图的格式为:
@extends('layouts.app')
@section('content')
<div class="card">
<div class="card-body">
<h4 class="card-title">Create a Company</h4>
</div>
<div class="container">
<div class="jumbotron">
<ul class="list-group">
<li class="list-group-item">
<h3>Enter Company Information:</h3>
<form action="{{ route('company.store') }}" enctype="mutlipart/form-data" method="POST">
@csrf
<div class="form-group">
<input type="text" class="form-control" name="name" placeholder="Company name" value="{{ old('name') }}">
</div>
<div class="form-group">
<input type="text" class="form-control" name="email" placeholder="Email" value="{{ old('email') }}">
</div>
<div class="form-group">
<input class="form-control" type="file" name="logo">
</div>
<div class="form-group">
<input class="form-control" type="url" name="website" placeholder="Website" value="{{ old('website') }}">
</div>
<button class="btn btn-primary" type="submit">Add Company</button>
</form>
</li>
</ul>
</div>
</div>
</div>
@endsection
这是路线:
路线::post('/add', 'CompaniesController@store')->名称('store');
好吧,当我尝试提交此表单时发生的事情是 $file 变量总是返回 null:
Error
Call to a member function getClientOriginalExtension() on null
http://localhost:8000/company/add
基本上我想做的是将图像名称发送到数据库并将图像上传到我的 public 文件夹。这段代码都不会发生。当我删除从 $file 到 dd($path) 的部分时;它将值添加到数据库,但图像尚未上传。
有什么帮助吗?提前致谢。
尝试更改为:
public function store(Request $request)
{
$file = $request->file('logo');
$path = '';
if($file) {
$filename = 'company-logo-' . time() . '.' . $file->getClientOriginalExtension();
$path = $file->storeAs('public', $filename);
}
Company::create([
'name' => $request->name,
'email' => $request->email,
'logo' => $path,
'website' => $request->website
]);
return redirect('/company/all');
}
if (Input::hasFile('logo')) {
$file = Input::file('logo');
$ext = $file->getClientOriginalExtension();
$file_name = 'company-logo-' . time() . ".{$ext}";
$path = base_path().'/public/';
$file->move($path , $file_name);
}
首先表格上的属性是错误的enctype="mutlipart/form-data"
,应该是enctype="multipart/form-data"
或者您可以根据需要使用以下代码的替代方法:
if($request->hasFile('logo')){
$file = $request->file('logo');
$fileName = 'company-logo-' .time().$file->getClientOriginalName();
Storage::put('public/'.$fileName,file_get_contents($file));
now you can store the $filename variable in database and image will be uploaded to storage/app/public folder
}
请在文件顶部添加 use Storage
和 运行 php artisan storage:link
以在存储文件夹和 public 文件夹
之间创建符号 link
首先,您在表单中更改 enctype="multipart/form-data"
而不是 enctype="mutlipart/form-data"
。
然后将此代码放入您的控制器
public function store(Request $request)
{
if($request->hasFile('logo')) {
$img_ext = $request->file('logo')->getClientOriginalExtension();
$filename = 'company-logo-' . time() . '.' . $img_ext;
$path = $request->file('logo')->move(public_path(), $filename);//image save public folder
}
//You should store only filename not path in db
Company::create([
'name' => $request->name,
'email' => $request->email,
'logo' => $filename,
'website' => $request->website
]);
return redirect('/company/all');
}
您可以使用文件池。核心库是用 vanilla JavaScript 编写的,因此可以在任何地方使用。 访问: https://pqina.nl/filepond/
这个函数是我用来存储新公司的函数:
public function store(Request $request)
{
$file = $request->file('logo');
$filename = 'company-logo-' . time() . '.' . $file->getClientOriginalExtension();
$path = $file->storeAs('public', $filename);
dd($path);
Company::create([
'name' => $request->name,
'email' => $request->email,
'logo' => $request->logo,
'website' => $request->website
]);
return redirect('/company/all');
}
此视图的格式为:
@extends('layouts.app')
@section('content')
<div class="card">
<div class="card-body">
<h4 class="card-title">Create a Company</h4>
</div>
<div class="container">
<div class="jumbotron">
<ul class="list-group">
<li class="list-group-item">
<h3>Enter Company Information:</h3>
<form action="{{ route('company.store') }}" enctype="mutlipart/form-data" method="POST">
@csrf
<div class="form-group">
<input type="text" class="form-control" name="name" placeholder="Company name" value="{{ old('name') }}">
</div>
<div class="form-group">
<input type="text" class="form-control" name="email" placeholder="Email" value="{{ old('email') }}">
</div>
<div class="form-group">
<input class="form-control" type="file" name="logo">
</div>
<div class="form-group">
<input class="form-control" type="url" name="website" placeholder="Website" value="{{ old('website') }}">
</div>
<button class="btn btn-primary" type="submit">Add Company</button>
</form>
</li>
</ul>
</div>
</div>
</div>
@endsection
这是路线:
路线::post('/add', 'CompaniesController@store')->名称('store');
好吧,当我尝试提交此表单时发生的事情是 $file 变量总是返回 null:
Error
Call to a member function getClientOriginalExtension() on null
http://localhost:8000/company/add
基本上我想做的是将图像名称发送到数据库并将图像上传到我的 public 文件夹。这段代码都不会发生。当我删除从 $file 到 dd($path) 的部分时;它将值添加到数据库,但图像尚未上传。
有什么帮助吗?提前致谢。
尝试更改为:
public function store(Request $request)
{
$file = $request->file('logo');
$path = '';
if($file) {
$filename = 'company-logo-' . time() . '.' . $file->getClientOriginalExtension();
$path = $file->storeAs('public', $filename);
}
Company::create([
'name' => $request->name,
'email' => $request->email,
'logo' => $path,
'website' => $request->website
]);
return redirect('/company/all');
}
if (Input::hasFile('logo')) {
$file = Input::file('logo');
$ext = $file->getClientOriginalExtension();
$file_name = 'company-logo-' . time() . ".{$ext}";
$path = base_path().'/public/';
$file->move($path , $file_name);
}
首先表格上的属性是错误的enctype="mutlipart/form-data"
,应该是enctype="multipart/form-data"
或者您可以根据需要使用以下代码的替代方法:
if($request->hasFile('logo')){
$file = $request->file('logo');
$fileName = 'company-logo-' .time().$file->getClientOriginalName();
Storage::put('public/'.$fileName,file_get_contents($file));
now you can store the $filename variable in database and image will be uploaded to storage/app/public folder
}
请在文件顶部添加 use Storage
和 运行 php artisan storage:link
以在存储文件夹和 public 文件夹
首先,您在表单中更改 enctype="multipart/form-data"
而不是 enctype="mutlipart/form-data"
。
然后将此代码放入您的控制器
public function store(Request $request)
{
if($request->hasFile('logo')) {
$img_ext = $request->file('logo')->getClientOriginalExtension();
$filename = 'company-logo-' . time() . '.' . $img_ext;
$path = $request->file('logo')->move(public_path(), $filename);//image save public folder
}
//You should store only filename not path in db
Company::create([
'name' => $request->name,
'email' => $request->email,
'logo' => $filename,
'website' => $request->website
]);
return redirect('/company/all');
}
您可以使用文件池。核心库是用 vanilla JavaScript 编写的,因此可以在任何地方使用。 访问: https://pqina.nl/filepond/