php 如何在laravel中上传多张图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42643265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to upload multiple image in laravel
提问by Rojina
I am trying to upload multiple images in a single row in a database table and access them in a show page.I have tried this tutorial: laraveldaily.com/upload-multiple-files-laravel-5-4/but there two different tables are made and a relation is established.
我正在尝试在数据库表的单行中上传多个图像并在显示页面中访问它们。我尝试过本教程: laraveldaily.com/upload-multiple-files-laravel-5-4/但有两个不同的表建立关系。
I want this to happen in a single table.
我希望这发生在一个表中。
回答by Suz Aann shrestha
here is what worked best for me:
这是最适合我的方法:
first do this in your form:
首先以您的形式执行此操作:
<form class="form-horizontal" enctype="multipart/form-data" method="post" action="/details">
and this for multiple selection:
这用于多项选择:
<input required type="file" class="form-control" name="images[]" placeholder="address" multiple>
Now do this in your controller:
现在在您的控制器中执行此操作:
public function store(request $request) {
$input=$request->all();
$images=array();
if($files=$request->file('images')){
foreach($files as $file){
$name=$file->getClientOriginalName();
$file->move('image',$name);
$images[]=$name;
}
}
/*Insert your data*/
Detail::insert( [
'images'=> implode("|",$images),
'description' =>$input['description'],
//you can put other insertion here
]);
return redirect('redirecting page');
}
Hope this works
希望这有效
回答by rashedcs
<form role="form" method="post" action="{{URL::to('addimage')}}" enctype="multipart/form-data">
<div class="form-group" style="padding-bottom: 15px">
<label class="col-lg-3">Upload</label>
<input class="btn btn-primary" type="file" name="files[]" > <br/>
</div>
</form>
$images = $request->file('files');
if ($request->hasFile('files')) :
foreach ($images as $item):
$var = date_create();
$time = date_format($var, 'YmdHis');
$imageName = $time . '-' . $item->getClientOriginalName();
$item->move(base_path() . '/uploads/file/', $imageName);
$arr[] = $imageName;
endforeach;
$image = implode(",", $arr);
else:
$image = '';
endif;
DB::table('fooo')->insert(
array(
'image' => $image
)
);
Session::flash('message', 'Image upload successfully successfully');
return redirect('/addimage');
回答by Fhulufhelo Mokhomi
this can also be achieved with
这也可以通过
foreach ($request->file('files') as $index => $item) {
$path = $request->files[$index]->store('images');
}