尝试上传图库时出现getClientOriginalName()错误

时间:2018-04-09 10:45:59

标签: php image laravel gallery

我目前正在学习laravel,而且我仍然坚持上传图片库&标题图像到特定文件夹。这是我的控制器代码:(我在&#34上编辑图像;编辑"页面)

public function update(Request $request, $id)
{

    $findObject = Accommodation::find($id);
    $findObject->update($request->all());

    Gallery::destroy('objects_id', $id);
    foreach ($request['img'] as $img) {
        $gallery = new Gallery();
        $gallery->objects_id=$id;
        $gallery->img=$img;
        $gallery->save();


        $file[0] = $request->file;

        $name = time() . $file[0]->getClientOriginalName(); // prepend the time (integer) to the original file name

        $file[0]->move('uploads', $name); // move it to the 'uploads' directory (public/uploads)

        // // create instance of Intervention Image
        $img = Image::make('uploads/'.$name)->resize(300,200);
        $img->save(public_path().'/uploads/'.$name);

    }

这是我上传图库和视频的观点。标题图片:

<div class="form-group">
<label for="exampleInputFile">Index image:</label>
<input type="file" name="headerImage" value="{{$object->headerImage}}">
<img src="{{asset('FrontAssets/img/smjestaj/')}}/{{$object->headerImage}}" style="width: 100px;">

<p class="help-block">Image that will be displayed on index page.</p>

<div class="form-group">
<label for="exampleInputFile">Image gallery</label>
<input type="file" name="img[]" multiple>

<p class="help-block">Choose x images for that will be displayed on gallery page.</p>

它显示了我的错误:

  

在null

上调用成员函数getClientOriginalName()

注意:我有&#34; enctype =&#34; multipart / form-data&#34;在我的形式。

This is what I get when i do dd();

我希望我能正确描述我的问题,如果我需要添加其他内容请告诉我。提前谢谢!

1 个答案:

答案 0 :(得分:0)

根据您的HTML

<input type="file" name="headerImage" value="{{$object->headerImage}}">

<input type="file" name="img[]" multiple>

尝试更改

foreach ($request['img'] as $img) {
            $gallery = new Gallery();
            $gallery->objects_id=$id;
            $gallery->img=$img;
            $gallery->save();


            $file[0] = $request->file;

            $name = time() . $file[0]->getClientOriginalName(); // prepend the time (integer) to the original file name

            $file[0]->move('uploads', $name); // move it to the 'uploads' directory (public/uploads)

            // // create instance of Intervention Image
            $img = Image::make('uploads/'.$name)->resize(300,200);
            $img->save(public_path().'/uploads/'.$name);

        }

if($request->file('img'))
 {
    foreach ($request->file('img') as $key => $file) {
       $gallery = new Gallery();
       $gallery->objects_id=$id;
       $gallery->img=$file;
       $gallery->save();

       $name = time() . $file->getClientOriginalName();
       $destination = base_path() . '/public/uploads';
       $file->move($destination, $name);
     }
 }
if($request->file('headerImage'))
{
    $name = time() . $request->file('headerImage')->getClientOriginalName();
    $destination = base_path() . '/public/uploads';
    $request->file('headerImage')->move($destination, $name);
}