使用Laravel存储多个上传



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

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>

它给我这样的错误:

"类型错误:很少有参数无法发挥 filesystem filesystemadapter :: putfileas(),2在e: laragon www www www www www blog vendor laravel laravel framework src src src iLLUMITAND在第343行,至少3行预期◀"

"

因此,问题是我使用is_array检查它是否有多个文件的方式是正确的吗?第二,什么会导致这个问题?

将第三参数传递给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();

文档

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

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

问题很清楚。当她至少需要3时,您正在尝试使用2个参数调用" putfileas"函数。
只需在此处查看文档

so

相关内容

  • 没有找到相关文章

最新更新