Laravel 5.5 - 上传多个图片

时间:2017-11-15 04:33:54

标签: php laravel-5 image-uploading

我的控制器中有这个功能:

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';
    }

如果我上传了多张图片,则仅保存最后一张图片。

我试过了:

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;
    }

这让我错误:

  

方法保存不存在。

我g目结舌,似乎我没有用模型实例化它。但在这种情况下,如果它只是一个直接的文件上传,如何用模型实例化它呢?

laravel中上传多张图片的最佳方法是什么?

更新

在阅读@ kunal的回答后,我设法通过为文件名添加唯一编号来解决问题:

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 个答案:

答案 0 :(得分:1)

我想你可能错过了这部分

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

请注意file[]必须是数组,否则会only save the last image if upload multiple image

如果你的html部分是正确的,那么就这样使用,

foreach ($file as $photo) {
    $path = Storage::putFile('foldername', $photo);
}

答案 1 :(得分:1)

试试这段代码:

$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();

和你的HTML页面:

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

答案 2 :(得分:1)

可能你正在寻找这种东西: -

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 :(得分:0)

没有那么复杂。 从5.8版开始,您可以执行以下操作:

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

它将图像放入公用磁盘的images文件夹中。