php Laravel 5.3 多文件上传
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39846148/
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
Laravel 5.3 multiple file uploads
提问by Jamie
How can I upload multiple files in Laravel 5.3
.
If I try it with 1 image it works but multiple images are not uploaded.
如何上传多个文件Laravel 5.3
。如果我尝试使用 1 张图像,它可以工作,但未上传多张图像。
This is my code:
这是我的代码:
if($request->hasFile('attachment')) {
foreach($request->allFiles('attachments') as $file) {
$file->store('users/' . $user->id . '/messages');
}
}
回答by Jamie
It works now like this:
它现在是这样工作的:
$files = $request->file('attachment');
if($request->hasFile('attachment'))
{
foreach ($files as $file) {
$file->store('users/' . $this->user->id . '/messages');
}
}
I had to append []
after the value of the name
attribute, so:
我必须[]
在name
属性值之后追加,所以:
<input type="file" name="attachment[]" multiple>
回答by Mayank Pandeyz
Try some thing like this:
尝试这样的事情:
public function multiple_upload() {
// getting all of the post data
$files = Input::file('images');
// Making counting of uploaded images
$file_count = count($files);
// start count how many uploaded
$uploadcount = 0;
foreach($files as $file) {
$rules = array('file' => 'required');
//'required|mimes:png,gif,jpeg,txt,pdf,doc'
$validator = Validator::make(array('file'=> $file), $rules);
if($validator->passes()){
$destinationPath = 'uploads';
$filename = $file->getClientOriginalName();
$upload_success = $file->move($destinationPath, $filename);
$uploadcount ++;
}
}
}
回答by Rashinda Sandaru
Controller.Php/ Laravel 5.7.28
Controller.Php/ Laravel 5.7.28
$files = $request->file('product_image_id');
if($request->hasFile('product_image_id')){
foreach ($files as $file) {
$name = time(). $file->getClientOriginalName();
$file->move('images',$name);
$productImage = ProductImage::create(['image'=>$name]);
$input ['product_image_id'] = $productImage->id;
}
}
回答by Edeeson Opina
Solved it with this one a simplier approach. Just make sure that your input file type is like this <input type="file" name="images[]" multiple>
用这个更简单的方法解决了这个问题。只要确保您的输入文件类型是这样的<input type="file" name="images[]" multiple>
$i = 0;
foreach($request->file('images') as $file){
$photo = new Photo;
// name it differently by time and count
$imageName = time() . $i . '.' . $file->getClientOriginalExtension();
// move the file to desired folder
$file->move('folderName/', $imageName);
// assign the location of folder to the model
$photo->image = 'folderName/' . $imageName;
$photo->status = 1;
$photo->save();
$i++;
}
回答by Malki Mohamed
If you want to still working with blade you can use this:
如果你想仍然使用刀片,你可以使用这个:
{{ Form::open(array('url' => 'upload', 'files'=>true)); }}
{{ Form::file('gallery[]', array('multiple'=>true,'accept'=>'image/*')); }}
{{ Form::submit(); }}
{{ Form::close(); }}
And in your Controller :
在你的控制器中:
files = $request->file('gallery');
if($request->hasFile('gallery'))
{
foreach ($files as $file) {
// $file->store('users/' . $this->user->id . '/messages');
//dump($file);
}
}