Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.3 multiple file uploads

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');
    }
}
like image 916
Jamie Avatar asked Oct 04 '16 07:10

Jamie


3 Answers

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>
like image 124
Jamie Avatar answered Oct 19 '22 18:10

Jamie


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>
like image 13
Isuru U Liyana Arachchi Avatar answered Oct 19 '22 16:10

Isuru U Liyana Arachchi


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++;
}
like image 4
Edeeson Opina Avatar answered Oct 19 '22 16:10

Edeeson Opina