Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

henryoladj's avatar

Multiple Image Upload System

I am trying to have a multiple image upload system but it is only one image that is showing after doing this

Form

<form action="{{route('news.store')}}" id="postform" method="post" enctype="multipart/form-data">
  {{csrf_field()}}
<div class="form-group">
  <label for="image"><b>Select Image To Add</b></label>
  <input type="file" name="image">
  <input type="file" name="image">
  <input type="file" name="image">
  <input type="file" name="image">
</div>
</form>

Controller

 public function store(Request $request)
    {
        //validate

        $this->validate($request,[
            'subject'=>'required|min:10',
            'tags' => 'required',
            'body' => 'required|min:20'

        ]);

        if (request()->hasFile('image')){

            request()->validate([
                'image' => 'file|image|max:5000',
            ]);

        }
        
        //store
        

        $news=auth()->user()->news()->create($request->all());

        $news->tags()->attach($request->tags);

        $this->storeImage($news);


        
        //redirect
        return redirect()->route('news.index');
    }

private function storeImage($news)
    {
        if (request()->has('image')){
            $news->update([
                'image' => request()->image->store('uploads', 'public'),
            ]);
        }
    }
0 likes
16 replies
FatihKececi's avatar

you should try this ;

<input type="file" name="image[]">

you only need to add one input for multiple file uploads.

FatihKececi's avatar

You should use in a foreach for multiple uploads and update your store method. Take a look at following example;

        request()->validate([
 
            'image' => 'required',
            'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
 
        ]);
 
        if ($image = $request->file('image')) {
            foreach ($image as $file) {
                    $destinationPath = 'public/image/'; // upload path
                    $image_name = date('YmdHis') . "." . $file->getClientOriginalExtension();
                    $file->move($destinationPath, $image_name);
            }
        }
henryoladj's avatar

Does not work

 private function storeImage($news)
    {
        if (request()->has('image')){
            foreach($image as $file){
                $name = time().'.'.$file->extension();
            $news->update([
                'image' => request()->image->store('uploads', 'public'),
            ]);

        }
        }
    }
henryoladj's avatar

I am talking about uploading more than one images

henryoladj's avatar

i am getting this error

Array to string conversion
if (request()->hasFile('image')){

            request()->validate([
                'image.*' => 'file|image|max:5000',
            ]);

        }
private function storeImage($news)
    {
        if (request()->has('image')){
            foreach ($image as $file) {
                $news->update([
                'image' => request()->image->store('uploads', 'public'),
            ]);
            }
            
        }
    }
ftiersch's avatar
foreach (request()->file('image') as $file) {
                $news->update([
                'image' => request()->image->store('uploads', 'public'),
            ]);
            }

You need to define the $image variable if you want to use it but you can also skip that step.

henryoladj's avatar

Can someone please help

Controller

public function store(Request $request)
    {
        //validate

        $this->validate($request,[
            'subject'=>'required|min:10',
            'tags' => 'required',
            'body' => 'required|min:20',
            'filename' => 'sometimes',
            'filename.*' => 'file|image|mimes:jpeg,png,jpg,gif,svg|max:5000'

        ]);

         
        
        //store
      
        $news=auth()->user()->news()->create($request->all());

        $this->storeImage($news);

        $news->tags()->attach($request->tags);

        
        //redirect
        return redirect()->route('news.index');
    }
 private function storeImage($news)
    {
       if (request()->has('image')) {
           
                foreach (request()->file('image') as $file) {
                $news->update([
                'image' => request()->image->store('uploads', 'public'),
            ]);
            }
        }
    }

View

<div class="form-group">
  <label for="image"><b>Select Image To Add</b></label>
  <input type="file" name="filename[]">
</div>

I get this error Array to string conversion

danielrubango's avatar

Hey @henryoladj,

I notice that your image input is named filename, so you have to take images from

request()->file('filename');

I think it will solve your issue.

jlrdw's avatar

An older example where 4 images was allowed just example.

        if (isset($_POST['submit'])) {
            $lid = DB::table('recents')->count();
            $k = -1;
            if (empty($lid) || strlen($lid) == 0 || is_null($lid)) {
                $lid = 1;
            }
            $newname = '';
            $destinationPath = ROOTDIR . 'upload/imgrecent/';
            $files = Input::file('ufile');
            //$names = [];
            $arrname = [];
            foreach ($files as $file) {
                $k = $k + 1;
                if (empty($file)) {
                    $arrname[$k] = "";
                } else {
                    $file_name = $file->getClientOriginalName();
                    $file_ext = $file->getClientOriginalExtension();
                    $lid = $lid + $k + 1;
                    $fileInfo = pathinfo($file_name);
                    $filename = $fileInfo['filename'];
                    // print_r($filename);
                    $arrname[$k] = $filename . $lid . "." . $file_ext;
                    $file->move($destinationPath, (string) $arrname[$k]);
                }
            }
            $pic1 = Cln::fixValue((string) $arrname[0]);  // you validate
            $pic2 = Cln::fixValue((string) $arrname[1]);
            $pic3 = Cln::fixValue((string) $arrname[2]);
            $pic4 = Cln::fixValue((string) $arrname[3]);
            $comments = Cln::fixValue($_POST['comments']);
           
                $postdata = array(
                    'pic1' => $pic1,
                    'pic2' => $pic2,
                    'pic3' => $pic3,
                    'pic4' => $pic4,
                    'comments' => $comments
                );
                DB::table('recents')->insert($postdata);
            
        }

Ingnore the:

Cln::fixValue

It's just a custom function where I strip_tags (sanitize)

this line:

$destinationPath = ROOTDIR . 'upload/imgrecent/';

Your destination path. Change Input::file to laravel request, this was an older version example.

$files = $request->input('ufile');

But it works.

The form:

    <form action='' method='post' enctype="multipart/form-data">
        <?php
        $num = 0;
        $num_uploads = 4;

        while ($num < $num_uploads) {
            echo '<div><input name="ufile[]" type="file" id="ufile[]" size="50" /></div>';
            $num++;
        }
        ?>

        <p>Comments</p><br>
        <textarea class="rightdiv" name='comments'></textarea>


        
        <p><input type='submit' name='submit' value='submit'></p>
    </form>

Adjust to laravel 6 syntax and blade as needed.

And put in csrf token.

Please or to participate in this conversation.