How to upload multiple images on laravel

legendary-tony

May 30th, 2018 03:07 PM

Controller:

public function store(Request $request)
{
    if (!empty($request->file('file'))) {
        $file_count = count($request->file('file'));
    } else {
        $file_count = null;
    }

    foreach (range(6, $file_count) as $index) {
        $rules['file.' . $index] = 'image|mimes:jpeg,gif,webp,bmp,png|max:2048';
    }

    $validator = Validator::make(Input::all(), $rules);

    if ($validator->fails()) {
        return back()
            ->withErrors($validator)
            ->withInput();
    } else {
        $description = $request->input('description');
        $category = $request->input('category');
        $privacy = $request->input('privacy');
        $file = $request->file('file');
        $facility = $request->input('facility');

        foreach ($file as $file1) {
            $file1->move(public_path() . '/images/', $file1->getClientOriginalName());

            $url = URL::to("/") . '/images/' . $file1->getClientOriginalName();

            DB::table('images_new')->insert([
                'description' => $description,
                'category' => $category,
                'privacy' => $privacy,
                'filename' => $url,
                'property_id' => $id,
                'created_at' => Carbon::now(),
                'updated_at' => Carbon::now(), // 'facilities' => $facility/ needs to be checked, it looks like an error
            ]);
        }

        return redirect('settings/photos');
    }
}

Blade:

<input type="file" name="file[]" class="upload-label" id="file" multiple />

Pls help me...if I upload one file iit will work but to upload multiple files..it would not upload

bobbyiliev

Jun 28th, 2023 07:26 AM

Hi there,

I am just following up on some of the old unanswered questions on the site.

Your foreach loop for validation rules seems to start from 6, which means it will ignore the first five files. If you want to validate each file, it should start from 0 or 1 depending on the array indexing.

The line 'facilities' => $facility/ seems like it could be an error. If facility is a column in your database, it should be 'facilities' => $facility,.

You didn't show where you get the $id variable. Make sure that it is defined and is getting the correct value.

After fixing these issues, your multiple file upload should work correctly.

Here is the updated controller:

public function store(Request $request)
{
    $files = $request->file('file');
    $rules = [];

    if($files){
        foreach ($files as $index => $file) {
            $rules['file.' . $index] = 'image|mimes:jpeg,gif,webp,bmp,png|max:2048';
        }
    }

    $validator = Validator::make($request->all(), $rules);

    if ($validator->fails()) {
        return back()
            ->withErrors($validator)
            ->withInput();
    } else {
        $description = $request->input('description');
        $category = $request->input('category');
        $privacy = $request->input('privacy');
        $facility = $request->input('facility');

        $id = "YourValue"; // TODO: Replace "YourValue" with actual value.

        foreach ($files as $file) {
            $filename = $file->getClientOriginalName();
            $file->move(public_path('images'), $filename);

            $url = url('images/' . $filename);

            DB::table('images_new')->insert([
                'description' => $description,
                'category' => $category,
                'privacy' => $privacy,
                'filename' => $url,
                'property_id' => $id,
                'created_at' => Carbon::now(),
                'updated_at' => Carbon::now(),
                'facilities' => $facility, // Assuming "facilities" is a valid column in your database
            ]);
        }

        return redirect('settings/photos');
    }
}

The HTML:

<input type="file" name="file[]" class="upload-label" id="file" multiple />

This code should work for uploading multiple files as you intend. Please replace "YourValue" with the appropriate value for the $id. And as always, make sure that the column names used match the ones in your actual database.