Laravel 5.3 multiple file uploads
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>
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>
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++;
}