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條經驗 獲得超5個贊
嘗試更改為:
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 關注
- 198 瀏覽
添加回答
舉報