【问题标题】:Laravel 5.5 - multiple images uploadLaravel 5.5 - 多张图片上传
【发布时间】:2018-04-28 04:56:24
【问题描述】:

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

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

这给我带来了错误:

方法保存不存在。

我目瞪口呆,似乎我没有用模型实例化它。但是在这种情况下,如果只是直接上传文件,如何用model实例化呢?

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

【问题讨论】:

  • 您想为单个用户提供多张图片吗?没有重复ID?

标签: php laravel-5 image-uploading


【解决方案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 />

【讨论】:

  • 你的代码不是我想要的,但是str_random(5) 确实解决了我的问题。我只是保留自己的代码,然后将$count 添加到循环中的$filename$count++; 中,我的问题就解决了。请检查我的更新。
  • @sooon hehehh 很高兴能帮助我的 str_random 帮助你:P
【解决方案2】:

我想你可能会错过这部分

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

【讨论】:

  • OP什么时候添加html代码的?你怎么知道 OP 错过了?
  • 我猜到了,他提到了which only save the last image
  • @arunkumar 你的回答不是我的问题,但我认为很有价值,因为像我这样的初学者总是会错过一些错误。
  • 是的,明白了!
【解决方案3】:

试试这个代码:

$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[]" />

【讨论】:

  • 你是怎么做到的? make($file)->resize(800,400);用你的代码?
  • 我正在尝试这种方式并保存调整大小的图像
  • foreach($files as $file){ $fullname = time() . '。' . $file->getClientOriginalExtension(); $path = public_path('images/' . $imageName); Image::make($image->getRealPath())->resize(800,400) ->save($path); $images[]=$全名; $has= implode(",",$images); }
【解决方案4】:

没那么复杂。 从 larval 5.8 开始,您可以这样做:

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

将图片放入公共磁盘的images文件夹中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-05
    • 2018-06-13
    • 2017-07-27
    • 2021-12-22
    • 2019-12-25
    • 2020-03-20
    • 2015-11-18
    • 2020-03-23
    相关资源
    最近更新 更多