duanlieshuang5330
2017-11-15 04:33
浏览 292
已采纳

Laravel 5.5 - 上传多个图片

I have this function in my controller:

use File;
use Image; //image intervention library
...
public function upload(Request $request)
    {
        //make sure there is a folder in public with the username
        $username = Auth::user()->name;
        $folderpath = public_path('images/' . $username . '/');
        File::makeDirectory($folderpath, $mode = 0777, true, true);

        $files = $request->file;

        if(!empty($files)):
            foreach($files as $file):
                $filename = 'post_' . time() . '.' . $file->getClientOriginalExtension();
                $path = $folderpath . $filename;
                Image::make($file)->resize(800,400)->save($path);
            endforeach;
        endif;

        return 'success';
    }

which only save the last image if I upload multiple.

and I tried:

public function upload(Request $request)
    {
        //make sure there is a folder in public with the username
        $username = Auth::user()->name;
        $folderpath = public_path('images/' . $username . '/');
        File::makeDirectory($folderpath, $mode = 0777, true, true);

        $files = $request->file;

        if(!empty($files)):
            foreach($files as $file):
                $filename = 'post_' . time() . '.' . $file->getClientOriginalExtension();
                $path = $folderpath . $filename;
                $file->save($path);
            endforeach;
        endif;

        return ''success;
    }

which throw me error:

Method save does not exist.

I goggled and it seems like I did not instantiate it with model. But in this case, how can I instantiate it with model if it is just a direct file upload?

What is the best way for multiple images upload in laravel?

Update

After reading @kunal's answer, I managed to solve the issue by adding a unique number for the file name:

public function upload(Request $request)
    {
        //make sure there is a folder in public with the username
        $username = Auth::user()->name;
        $folderpath = public_path('images/' . $username . '/');
        File::makeDirectory($folderpath, $mode = 0777, true, true);

        $files = $request->file;
        $count = 0;//<-- add a counter
        if(!empty($files)):
            foreach($files as $file):
                $filename = 'post_' . time() . '_' . $count . '.' . $file->getClientOriginalExtension();//<-- add counter to the file name
                $path = $folderpath . $filename;
                Image::make($file)->resize(800,400)->save($path);
                $count ++;//<-- increase the value
            endforeach;
        endif;

        return 'success';
    }
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dougai0138 2017-11-15 05:02
    已采纳

    May be you are looking for this kind of stuff:-

    if ($request->hasFile('files')) {
    $files = $request->file('files');
    foreach($files as $file){
        $extension = $file->getClientOriginalExtension();
        $fileName = str_random(5)."-".date('his')."-".str_random(3).".".$extension;
        $folderpath  = 'images'.'/';
        $file->move($folderpath , $fileName);
    }
    }
    <input type="file" id="gallery" name="files[]" multiple />
    
    已采纳该答案
    打赏 评论
  • dongse5528 2017-11-15 04:46

    I am thinking u may missed this part

    <input type="file" id="gallery" name="file[]" />
    

    note the file[] it must be array otherwise it will only save the last image if upload multiple image

    If u did ur html part correct, then use like this,

    foreach ($file as $photo) {
        $path = Storage::putFile('foldername', $photo);
    }
    
    打赏 评论
  • doujianzi8521 2017-11-15 04:59

    try this code:

    $files= Input::file('image');
    $destinationPath= 'images';
    $images=array(); 
    foreach($files as $file){
        $fullname= $file->getClientOriginalName(); 
        $hashname  = $fullname; 
        $upload_success   =$file->move($destinationPath, $hashname);
        $images[]=$fullname;
        $has= implode(",",$images);
    }
    $modelname= new Modelname;
    $modelname->image_attachment    =  $has;  
    $modelname->save();
    

    and yout html page:

    <input type="file" id="image" name="image[]" />
    
    打赏 评论
  • doubengman2072 2019-07-14 11:06

    It is not that complicated. As of larval 5.8 you can do this:

    collect($request->images)->each(function ($image) {
        return $image->store('images', 'public');
    });
    

    It puts images in images folder of public disk.

    打赏 评论

相关推荐 更多相似问题