Lgendary's avatar

How to upload multiple file on laravel

`

$request->file('file'); if (!empty($request->file('file'))) { $file_count = count($request->file('file')); $rules['file.' . $index] = 'image|mimes:jpeg,gif,webp,bmp,png|max:2048'; $facility = $request->input('facility'); $i = 0; foreach ($file as $file1) { $i++; $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*/]); } return redirect('settings/photos'); } ` }

THAT IS CONTROLLER
            

`

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

0 likes
6 replies
jlrdw's avatar

Upload multiple is no different that one. You have to foreach through them.

You code hard to read, please reformat correctly.

Lgendary's avatar

here

$request->file('file'); 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'; }

panditya's avatar

I think you should remove some line of code that no need, for example, the count because you are using 'foreach' so you don't need it but if you want to use count then use 'for' instead. And maybe better use true timestamps in the model rather than use Carbon every execution to the database. For file handling, I suggest using Laravel File Storage Maybe like this

...
use App\Image;
use Storage;
...
    // check for the request
    if (!empty($request->image)) {
      // check how many requests come
      if (count($request->image) == 1) {
        // do single upload 
        $uploadedFile = $request->file('file'); // be careful naming your request, I suggest use image or others..
        $filelink = $uploadedFile->storeAs('public/images', time().'.'.$request->cover->getClientOriginalExtension()); // change the time() if you want to use the original name but I didn't recommend it because avoiding duplicated file
        // do with your own style to store
        $image = new Image;
        $image->description = $request->description;
        $image->category = $request->category;
        $image->privacy = $request->privacy;
        $image->filename = $filelink; // to get the file path 
        $image->property_id = $request->id; // idk what is this 
        if ($image->save()) {
            return redirect('settings/photos');            
        }
    // or something else
      }
      else {
        // for multiple request use foreach instead 
        foreach ($request->image as $item) {
          // do upload and store
          $uploadedFile = $request->file('file'); // be careful naming your request, I suggest use image or others..
          $filelink = $uploadedFile->storeAs('public/images', time().'.'.$request->cover->getClientOriginalExtension()); //  change the time() if you want to use the original name but I didn't recommend it because avoiding duplicated file
          // do with your own style to store
          $image = new Image; 
          $image->description = $request->description;
          $image->category = $request->category;
          $image->privacy = $request->privacy;
          $image->filename = $filelink; // to get the file path 
          $image->property_id = $request->id; // idk what is this 
          if ($image->save()) {
              return redirect('settings/photos');            
          }
    // or something else
        }
      }
      // or something else
    }

It's my style btw, I haven't tested it out hope it's work haha

Cronix's avatar

Please people, learn to format your code. 3 backticks on their own line, followed by code, followed by 3 backtacks on their own line.

```

your code here...

```

jake.admin@gmail.com's avatar

Did you tell your Form you'll be uploading files and multiple files? {{Form::open(['route'=>'uploadImage', 'files' => true])}} {{ Form::file('photo[]', ['multiple', 'class'=>'form-control']) }}

Please or to participate in this conversation.