arslan2037's avatar

Multiple files upload in laravel

I want to upload multiple files and save their path into one column using comma separator, i am trying it but not get 100% result.

Right now images or files are uploaded into path but it save only first file name in db.

here is my form

    <form role="form" method="post" action="{{ action('ProjectsController@projectCommunication') }}" enctype="multipart/form-data">
             <input type="hidden" name="_token" value="{{ csrf_token()}}">
            
            <div class="form-group">
                <label for="message">Enter Comment:</label>
                <textarea class="form-control" rows="3" id="message" name="message" placeholder="Enter Comment here!"></textarea>
            </div>
            
            <div class="form-group">
                <label for="media">Upload File:</label>
                <input type="file" class="form-control" name="media[]" id="media" multiple>
            </div>

    <button class="btn btn-primary">Send</button>
        </form>

here is controller code

    if($request->file('media'))
    {
        foreach($request->file('media') as $media)
        {
            if(!empty($media))
            {
            $destinationPath = 'uploads/companies/award';
            $filename = $media->getClientOriginalName();
            $media->move($destinationPath, $filename);
            
            $projectcommunication->media = $filename;
            }
        }
        }
0 likes
7 replies
Tangente's avatar
Tangente
Best Answer
Level 9

Try $projectcommunication->media. = ",".$filename; which is the same as $projectcommunication->media = $projectcommunication->media.",".$filename;

1 like
arslan2037's avatar

now working but only a small issue is that it start with , it

zachleigh's avatar

Put all the filenames in an array and use implode(', ', $array) to make a comma separated string.

arslan2037's avatar

$doc_Ids = implode(',', $request->file('media'));

$projectcommunication->media = $doc_Ids;

it save file names like this every time, C:\xampp\tmp\phpAB1C.tmp,C:\xampp\tmp\phpAB3C.tmp

Please or to participate in this conversation.