2

I have a form in which user should at least select one file to be uploaded. I have three file input fields like this:

            <div class="form-group col-lg-4">
                {!! Form::label('file1', 'Select file 1', ['class' => 'control-label']) !!}
                {!! Form::file('files[]', ['id'=>'file1']) !!}
            </div>
            <div class="form-group col-lg-4">
                {!! Form::label('file2', 'Select file 2', ['class' => 'control-label']) !!}
                {!! Form::file('files[]', ['id'=>'file2']) !!}
            </div>
            <div class="form-group col-lg-4">
                {!! Form::label('file3', 'Select file 3', ['class' => 'control-label']) !!}
                {!! Form::file('files[]', ['id'=>'file3']) !!}
            </div>

I should validate the presence of at least one file and the mime types in a form request. Then in the store method of the related form controller, the original file names should be stored in the three corresponding database fields(namely file1, file2, file3).

How can I implement this?

5
  • 1
    maybe useful? Upload multiple files in laravel 5. Commented Aug 11, 2015 at 8:25
  • @RyanVincent Thank you. But it didn't help me Commented Aug 11, 2015 at 9:35
  • @AliErfani why didn't the link help? It describes in details the process of uploading multiple files in Laravel and how to implement your store method Commented Aug 11, 2015 at 11:18
  • @jedrzej.kurylo It describes multiple file uploads from one field. I should validate each file separately then save the original name of each file to the database Commented Aug 11, 2015 at 11:24
  • 1
    You'll get the original name of each field using the method described in the link Commented Aug 11, 2015 at 12:04

1 Answer 1

4

After some searching around I finally came up with a solution. First of all I modified the view to look like this:

<div class="form-group col-lg-4">
            {!! Form::label('file1', 'Select file 1', ['class' => 'control-label']) !!}
            {!! Form::file('file1', ['id'=>'file1']) !!}
        </div>
        <div class="form-group col-lg-4">
            {!! Form::label('file2', 'Select file 2', ['class' => 'control-label']) !!}
            {!! Form::file('file2', ['id'=>'file2']) !!}
        </div>
        <div class="form-group col-lg-4">
            {!! Form::label('file3', 'Select file 3', ['class' => 'control-label']) !!}
            {!! Form::file('file3', ['id'=>'file3']) !!}
        </div>

Then in the controller I used your suggested code:

$files =[];
        if ($request->file('file1')) $files[] = $request->file('file1');
        if ($request->file('file2')) $files[] = $request->file('file2');
        if ($request->file('file3')) $files[] = $request->file('file3');
        foreach ($files as $file)
        {
            if(!empty($file)){
                $filename=$file->getClientOriginalName();
                $file->move(
                    base_path().'/public/uploads/', $filename
                );
            }

        }
Sign up to request clarification or add additional context in comments.

1 Comment

what if I have three different fields in database? So How i'm gonna 3 different images store it in database?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.