5 回答
TA贡献1111条经验 获得超0个赞
首先,表单上的属性是错误的,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以在存储文件夹和公共文件夹之间建立符号链接
TA贡献1866条经验 获得超5个赞
首先你改变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');
}
TA贡献1821条经验 获得超4个赞
尝试更改为:
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');
}
TA贡献1804条经验 获得超7个赞
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);
}
- 5 回答
- 0 关注
- 145 浏览
添加回答
举报