欢迎各位兄弟 发布技术文章
这里的技术是共享的
Ask QuestionAsked Active 10 months agoViewed 68k times27
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.
This is my code:
if($request->hasFile('attachment')) {
foreach($request->allFiles('attachments') as $file) {
$file->store('users/' . $user->id . '/messages');
}
}
phpfilelaravel-5ShareImprove this questionedited Feb 7 '20 at 16:56Max Base533 asked Oct 4 '16 at 7:05Jamie8,240
看下面的红色部分比较重要
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:
<input type="file" name="attachment[]" multiple>
ShareImprove this answeredited Nov 2 '17 at 2:37joshuamabina1,210 answered Oct 4 '16 at 7:45Jamie8,240
3that what i mention on your question Form::file('attachment[]', ['multiple' => 'multiple']); – channasmcs Oct 4 '16 at 11:01
1But this is not working for me. only the last file is being uploaded – Mutasim Fuad Apr 22 '17 at 18:00
@Jamie attachment[]
is not shown in your code? Do you mean it should be? – Don't Panic Sep 7 '17 at 9:10
This is Controller to upload multiple files in laravel:
public function fileUpload(Request $request)
{
if ($request->hasfile('filenames')) {
foreach ($request->file('filenames') as $file) {
$name = $file->getClientOriginalName();
$file->move(public_path() . '/mytestfile/', $name);
$data[] = $name;
}
return back()->with('Success!','Data Added!');
}
}
This view file in resources:
<html lang="en">
<head>
<title>Multiple Image Upload</title>
<script src="jquery/1.9.1/jquery.js"></script>
<link rel="stylesheet" href="3.3.6/css/bootstrap.min.css">
</head>
<body>
<div class="container lst">
<h3 class="well">Test Muliple Image Upload</h3>
<form method="post" action="{{url('image-upload')}}" enctype="multipart/form-data">
{{csrf_field()}}
<div class="input-group hdtuto control-group lst increment" >
<input type="file" name="filenames[]" class="myfrm form-control" multiple>
<div class="input-group-btn">
<button class="btn btn-success" type="button"><i class="fldemo glyphicon glyphicon-plus"></i>Add</button>
</div>
</div>
<button type="submit" class="btn btn-success" style="margin-top:10px">Submit</button>
</form>
</div>
<script type="text/javascript">
$(document).ready(function() {
$(".btn-success").click(function(){
var lsthmtl = $(".clone").html();
$(".increment").after(lsthmtl);
});
$("body").on("click",".btn-danger",function(){$(this).parents(".hdtuto control-group lst").remove();
});
});
</script>
</body>
</html>
ShareImprove this answeredited Jul 31 '20 at 8:14סטנלי גרונן2,740 answered Jul 31 '20 at 7:41umayanga isuru81
does this support a mp4/video and images in one post like Instagram? – Jm Macatangay Nov 9 '20 at 6:20
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>
$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++;
}
ShareImprove this answeredited Feb 7 '20 at 16:48chriscatfr2,492 answered Jul 5 '19 at 9:54Edeeson Opina21Add a comment1
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 ++;
}
}
}
ShareImprove this answeredited Dec 10 '18 at 7:00Tadas V.617 answered Oct 4 '16 at 7:10Mayank Pandeyz23.3k
1Thanks but this is not with the new features off Laravel 5.3 right? – Jamie Oct 4 '16 at 7:13
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;
}
}
ShareImprove this answeredited Feb 7 '20 at 11:45Max Base533 answered May 10 '19 at 19:58Rashinda Sandaru11Add a comment0
If you want to still working with blade you can use this:
{{ Form::open(array('url' => 'upload', 'files'=>true)); }} (<或者 form class="form-horizontal" enctype="multipart/form-data" action="" method="POST">)
{{ Form::file('gallery[]', array('multiple'=>true,'accept'=>'image/*')); }} (或者 <input type="file" class="form-control" name="photos[]" multiple />)
{{ 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);
}
}
ShareImprove this answeranswered Jan 22 '18 at 9:37Malki Mohamed1,052Add a comment0
This is how my blade looks like:
<div class="form-group row">
<label for="gallery-image" class="col-sm-3 text-left control-label col-form-label">Photo</label>
<div class="col-md-9">
{{-- <input type="file" name="images[]" multiple> --}}
<input type="file" class="form-control form-control-file"
id="gallery-image" value="{{ old('gallery-image') }}" required name="gallery-image[]" multiple/>
<span class="below-image-upload-msg">Advised image dimensions: 1920 x 450 pixels. File size should be less than 2Mb per image.</span>
</div>
@error('image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
And this is how the store function in the controller looks like:
public function store(Request $request){
$validation = Validator::make($request->all(), array(
'gallery-id' => 'required|integer|min:0',
'gallery-image' => 'required|image|nullable|mimes:jpeg,png,jpg,gif|max:2048',
));
$fileNameToStore="no-image.jpg";
if(request('gallery-image'))
{
$iCount=0;
foreach($request->file('gallery-image') as $thisImage){
$fileNameWithExtension = $thisImage->getClientOriginalName();
$memberName = Auth::user()->MemberProfile->member_tourist_office_name;
$extension=$thisImage->getClientOriginalExtension();
$fileNameToStore = $fileName.'_'.time().$iCount.'.'.$extension;
$path= $thisImage->storeAs('public/images/members/gallery-images',$fileNameToStore);
$isEnabled = "0";
$galleryImage = new GalleryImage();
$galleryImage->gallery_id = request('gallery-id');
$galleryImage->gallery_image_caption = " ";
$galleryImage->gallery_image = $fileNameToStore;
$galleryImage->isEnabled = $isEnabled;
$galleryImage->save();
$iCount++;
}
return redirect('/galleryimage/create')->with('success', 'Image(s) successfully added to gallery!');
}
}
ShareImprove this answeranswered Jul 4 '20 at 7:59hackernewbie633Add a comment
1did you add Form::file('myfile[]', ['multiple' => 'multiple']); – channasmcs Oct 4 '16 at 7:09
I'm testing with postman. So yes I have done that. – Jamie Oct 4 '16 at 7:12
@Jamie how do you save the paths now to DB?? – lewis4u Dec 2 '16 at 10:12