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

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 />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里