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

anikkhan's avatar

Upload Multiple Files

Hi, I am having some problem with upload multiple files. I uploaded multi files and my database its show the current number and its look good but in the file path its doesn't show all the file just only 2 files if i upload more then 2 .

here is my controller ''' public function postUploadImages(Request $request, Page $page) { // $this->validate($request, [ // 'path' => 'required|image|mimes:jpeg,bmp,png' // ]);

    if($request->hasFile('path')){
        $path = public_path(). '/uploads/images/';
        $files = $request->file('path');

        if($files[0] != ''){
            foreach ($files as $file){
                if (!File::exists($path)) File::makeDirectory($path, 0775, true);
                $name = md5(Carbon::now()). '.' . $file->getClientOriginalExtension();
                $file->move($path, $name);

                $image = new Image();
                $image->path = $name;
                $image->page_id = $page->id;
                $image->save();
            }
        }
    }

    return redirect()->route('backend.pages.image', $page)->with('success', 'Successfully upload Image');
}

''' Thank you

0 likes
1 reply
Sergiu17's avatar

Debug, dd($files), see what you get.

if($files[0] != ''){

What are you checking here?

Don't do this in foreach:

if (!File::exists($path)) File::makeDirectory($path, 0775, true);

actually, delete this row. $file->move($path, $name) will create a folder for you. You do not need to make a directory.

So, try to debug. Learn to do this all the time!

Please or to participate in this conversation.