使用Laravel Storage进行多次上传

时间:2017-12-06 13:47:29

标签: laravel laravel-5 laravel-storage

我尝试使用存储上传多个文件,所以这里是我的代码:

public function store(Request $request)
{
    $gallery  = new Gallery();
    $gallery->name = $request->name;
    $gallery->description = $request->description;
    $gallery->save();

    $path = 'public'.'/'.str_slug($request->name);
    Storage::makeDirectory(str_slug($request->name));

    $image = new Image();
    if ( is_array($request->images)) {
        foreach ($request->images as $file) {
            $image->gallery_id = $gallery->id;
            $image->name = $file->getClientOriginalName();
            $image->extension = $file->extension();
            $image->save();
            Storage::putFileAs($path, $file);
        }
    } else {
            $image->gallery_id = $gallery->id;
            $image->name = $request->images->getClientOriginalName();
            $image->save();
    }
}

我已经设置了这样的表格:

<div class="form-group">
    <label for="images" class="col-md-2 control-label">Images</label>
    <div class="col-md-6">
        <input type="file" class="form-control" name="images[]" id="images[]" multiple>
    </div>
</div>

它给我一个这样的错误:

  

&#34;类型错误:函数Illuminate \ Filesystem \ FilesystemAdapter :: putFileAs()的参数太少,2在E:\ laragon \ www \ blog \ vendor \ laravel \ framework \ src \ Illuminate \ Filesystem \中传递第343行的FilesystemManager.php和至少3个预期的◀&#34;

所以问题是我用is_array检查它有多个文件的方式是对的吗?什么可以导致这个问题?

3 个答案:

答案 0 :(得分:0)

您可以使用以下示例。我认为这可以帮助您解决问题

public function store(Request $request)
{
    $gallery  = new Gallery();
    $gallery->name = $request->name;
    $gallery->description = $request->description;
    $gallery->save();

    $path = '/images/'.str_slug($request->name);

    if( is_array($request->images)){
        foreach ($request->images as $file){
            $image = new Image();
            $image->gallery_id = $gallery->id;
            $image->name = $file->getClientOriginalName();
            $image->extension = $file->extension();
            $image->size = 555;
            $image->save();
            $file->move(public_path($path, $image->name);
        }
    }else{
        $image = new Image();
        $image->gallery_id = $gallery->id;
        $image->name = $request->images->getClientOriginalName();;
        $image->save();
        $file->move(public_path($slug, $image->name);
    }
    return back();
}

答案 1 :(得分:0)

将第3个参数传递给npm i dynamodb-data-types方法

wrap({ 'a': 'My string' })

Documentation

答案 2 :(得分:0)

问题很明显。您正试图调用函数&#34; putFileAs&#34;使用2个参数,当她至少需要3时 只需查看文档here

即可

所以