我想使用此代碼在Laravel上傳圖片:Laravel上傳多張圖片不能正常工作,也默劇不
public function postAddPictures(Request $request)
{
// getting all of the post data
$files = $request->file('cover_image');
// Making counting of uploaded images
$file_count = count($files);
// start count how many uploaded
$uploadcount = 0;
foreach($files as $file) {
$messages = [
'cover_image.required' => 'U moet een afbeelding opgeven.',
'cover_image.image' => 'De bestanden moeten een afbeelding zijn (jpeg, png, bmp, gif, or svg).',
'description.required' => 'U moet een beschrijving opgeven.'
];
$rules = [
'cover_image' => 'required',//|mimes:png,gif,jpeg,jpg,bpm,svg
'album_id' => 'required|numeric|exists:albums,id',
'description' => 'required'
];
$validate = ['file'=> $file, 'description' => $request->get('description'), 'album_id'=> $request->get('album_id')];
$validator = Validator::make($validate, $rules, $messages);
if ($validator->fails()) {
return Redirect::to('admin/pictures/add')->withInput()->withErrors($validator);
}
$random_name = str_random(8);
$destinationPath = 'public/uploads/pictures/rallypodium/website/'.Album::find($request->get('album_id'))->type.'/'.Album::find($request->get('album_id'))->name.'/';
$extension = $file->getClientOriginalExtension();
$filename = $random_name.'_album_image.'.$extension;
$uploadSuccess = $file->move($destinationPath, $filename);
Images::create([
'description' => $request->get('description'),
'image' => $filename,
'album_id'=> $request->get('album_id')
]);
$uploadcount ++;
}
if($uploadcount == $file_count){
Activity::log('heeft foto's in de map map "'.ucwords(str_replace('-', ' ', Album::find($request->get('album_id'))->name)).'" toegevoegd.');
$request->session()->flash('alert-success', 'Foto's succesvol toegevoegd.');
return Redirect::to('admin/pictures/add');
}
}
這裏的問題是,它使返回的錯誤信息「U現在就去看看。「它不會將數據存儲在數據庫中,也不會上傳文件。
這是我的字段HTML:
- cover_image
- album_id
- 描述
有人能幫助我嗎?我嘗試過不同的方式,但我找不到解決方案。
最親切的問候,
羅賓
我自己找到了解決方案。我需要驗證'$ request-> all()'而不是數組。 :$然而,這些啞劇根本不起作用。 – Robin