0

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

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>

它给了我这样的错误:

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

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

4

3 回答 3

1

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

文档

于 2017-12-06T14:12:01.800 回答
0

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

所以

于 2017-12-06T14:12:19.657 回答
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();
}
于 2017-12-06T14:04:44.520 回答