Laravel 5.2 中的多张图片上传
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36832358/
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
Multiple Image Upload in Laravel 5.2
提问by Putri Dewi Purnamasari
Finally I can upload and move the images, but now I want to create a multiple upload images on Laravel. Is that possible? Did I have to use array to make it?
最后我可以上传和移动图像,但现在我想在 Laravel 上创建多个上传图像。那可能吗?我必须使用数组来制作它吗?
Can I just modify a little bit from this code?
我可以从这段代码中稍微修改一下吗?
It's on my ProductController.php
它在我的 ProductController.php 上
$picture = '';
if ($request->hasFile('images')) {
$file = $request->file('images');
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$picture = date('His').$filename;
$destinationPath = base_path() . '\public\images/';
$request->file('images')->move($destinationPath, $picture);
}
if (!empty($product['images'])) {
$product['images'] = $picture;
} else {
unset($product['images']);
}
Thank you. Note: My code above is from a kindhearted person on stackoverflow, thanks again ;)
谢谢你。注意:我上面的代码来自stackoverflow上的一个好心人,再次感谢;)
回答by sohaib rehman
At your frontend form you'll have to use your field attribute name like
在您的前端表单中,您必须使用您的字段属性名称,例如
name="images[]"
And your controller code would be like this.
您的控制器代码将是这样的。
$picture = '';
if ($request->hasFile('images')) {
$files = $request->file('images');
foreach($files as $file){
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$picture = date('His').$filename;
$destinationPath = base_path() . '\public\images';
$file->move($destinationPath, $picture);
}
}
if (!empty($product['images'])) {
$product['images'] = $picture;
} else {
unset($product['images']);
}
回答by Adam
Your input from $_POST will be coming in as an array. All you need to do is to iterate through it:
您来自 $_POST 的输入将作为数组输入。您需要做的就是遍历它:
$picture = '';
if ($request->hasFile('images')) {
$files = $request->file('images');
foreach($files as $file){
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$picture = date('His').$filename;
$destinationPath = base_path() . '\public\images/';
$request->file('images')->move($destinationPath, $picture);
}
}
回答by rashedcs
Slight modified code to upload multiple image.
稍微修改代码以上传多个图像。
public function store(Request $request)
{
$pid = $request->input('pid');
$input = $request->file('images');
$picture = array();
if($request->hasFile('images')) :
foreach ($input as $item):
$extension = $item->getClientOriginalName();
$name = date('Ymd') . '.' . $extension;
$destinationPath = base_path() . '/uploads/images/';
$item->move($destinationPath, $name);
$arr[] = $name;
endforeach;
$picture = implode(",", $arr);
else:
$picture = '';
endif;
DB::table('document')->insert(array('pid' => $pid,'image' => $picture));
Session::flash('message', 'Multiple pictures are uploaded successfully');
return redirect('/image-upload');
}