使用 Laravel 存储进行多次上传

Multiple upload using 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>

它给我这样的错误:

"Type error: Too few arguments to function Illuminate\Filesystem\FilesystemAdapter::putFileAs(), 2 passed in E:\laragon\www\blog\vendor\laravel\framework\src\Illuminate\Filesystem\FilesystemManager.php on line 343 and at least 3 expected ◀"

所以问题是我使用 is_array 检查它是否有多个文件的方式是否正确?第二个是什么导致了这个问题?

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

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

将第三个参数传递给 putFileAs 方法

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

        // Or any custom name as the third argument
        Storage::putFileAs($path, $file, $file->getClientOriginalName());
    }
} else {
        $image = new Image();
        $image->gallery_id = $gallery->id;
        $image->name = $request->images->getClientOriginalName();;
        $image->save();

        // Or any custom name as the third argument
        Storage::putFileAs($path, $request->images, $request->images->getClientOriginalName());
}
return back();

Documentation

问题很清楚了。您正在尝试使用 2 个参数调用函数 "putFileAs",而她至少需要 3 个参数。
看看文档 here

所以