Laravel upload multiple files

本小妞迷上赌 提交于 2021-01-29 05:40:19

问题


I'm building a gallery with multiple files functionality.

So far I'm having two issues, but let's paste my code first.

Controller:

public function store(Request $request)
{

  $gallery = new GalleryImage();

  if($request->hasFile('image')) {
    $images = $request->file('image');
    foreach($images as $image) {
      $path = $image->getClientOriginalName();
      $name = time() . '-' . $path;
      $gallery->image = $image->move(public_path().'/uploads/temp/', $name);
      //$gallery->image = $request->file('image')->storeAs('public/gallery-images', $name);
    }
  }

  $gallery->gallery_id = $request->gallery_id;

  $gallery->save();

  return back()->with('success_message','Images has been uploaded!');
}

View blade:

    <form action="{{ route('gallery-images.store') }}" method="POST" enctype="multipart/form-data">
      {{ csrf_field() }}
      <div class="form-group">
        <input type="hidden" name="gallery_id" value="{{ $gallery->id }}">
        <input type="file" name="image[]" id="id_imageGallery" class="form-control" multiple="multiple" required>
      </div>
      <div class="form-group">
        <button type="submit" class="btn btn-primary">Upload</button>
      </div>
    </form>

My first issue when I have my code like this and upload three files, it's successfully storing the files into /uploads/temp directory, but in my database I can see there's only one image uploaded, instead of three.

My second issue is when I change my code and use the commented part (because I want to store those three images into Storage):

$gallery->image = $request->file('image')->storeAs('public/gallery-images', $name);

I'm getting this error:

Call to a member function storeAs() on array

How can I upload those multiple images into the Storage folder and record them all into the database?

-- EDIT -- I've solved my first issue!

I've simply put the save method and other details inside the loop. I've should have thought about this before :)

Here's my updated code:

public function store(Request $request)
{


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

    $images = $request->file('image');
    foreach($images as $image) {
      $gallery = new GalleryImage();
      $path = $image->getClientOriginalName();
      $name = time() . '-' . $path;
      $gallery->image = $image->move(public_path().'/uploads/temp/', $name);
      //$gallery->image = $request->file('image')->storeAs('public/gallery-images', $name);

      $gallery->gallery_id = $request->gallery_id;
      $gallery->save();
    }
  }


  return back()->with('success_message','Images has been uploaded!');
}

Now only thing to do is how to store them into File Storage, instead of the public_path and avoid that error:

Call to a member function storeAs() on array 

回答1:


Try to implement using the following code, it will give uploaded files url in serialized format

    public static function upload_file($file){
        $file_name = time();
        $file_name .= rand();
        $file_name = sha1($file_name);
        if ($file) {
            $ext = $file->getClientOriginalExtension();
            $file->move(public_path() . "/uploads", $file_name . "." . $ext);
            $local_url = $file_name . "." . $ext;
            $s3_url = url('/').'/uploads/'.$local_url;
            return $s3_url;
        }
        return "";
    }

    public static function upload_multiple_files($files){
        if(is_array($files)){
            $return_array = array();
            foreach ($files as $file){
                if(!empty($file)){
                    $return_array[] = self::upload_file($file);
                }else{
                    $return_array[] = '';
                }
            }
            return serialize($return_array);
        }else{
            return NULL;
        }
    }



回答2:


//I have updated and commented your code. Try it, It should work.

public function store(Request $request)
    {
      if($request->hasFile('image')) {

        $images = $request->file('image');
        foreach($images as $image) {
          $gallery = new GalleryImage();
          // laravel auto manages unique name and extension of file.
          $gallery->image = $image->store('gallery-images', 'public'); 
          $gallery->gallery_id = $request->gallery_id;
          $gallery->save();
        }
      }
      return back()->with('success_message','Images has been uploaded!');
    }



回答3:


I've finally solved the issues and I'm posting it, so it may be useful for some.

Here's my code:

public function store(Request $request)
{

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

    foreach($request->image as $image) {
      $path = $image->getClientOriginalName();
      $name = time() . '-' . $path;

      $gallery = new GalleryImage();
      $gallery->image = $image->storeAs('public/gallery-images', $name);
      $gallery->gallery_id = $request->gallery_id;
      $gallery->save();
    }
  }

  return back()->with('success_message','The images have been uploaded!');
}


来源:https://stackoverflow.com/questions/56108388/laravel-upload-multiple-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!