$ request-> file('image')结果为null

时间:2019-09-11 19:26:40

标签: php laravel laravel-5

我想将图像保存在目录中并在网页上检索它。我尝试了几种方法将图像存储在文件夹中,但出现此错误。 当我使用$request->file('image')时,结果是null 我找出解决方案并尝试作为解决方案。但我无法解决。 如何解决此错误以及如何将图像保存在文件目录中

 Controller

   public function dili(Request $request)
   {
    $di = new diligent;



    $di->jobtype = $request->jobtype;
    $di->jobC = $request->jobC;
    $di->details = $request->details;
    $di->image = $request->image;



    $imag =  $request->image;

    //dd($imag);
    $path = $imag->getClientOriginalName();

    $destinationPath = public_path('img');

    $imag->move($destinationPath, $path);


    $di->save();
    $de = diligent::all();
    return view('admin')->with('dw', $de);
}

web.php

 Route::post('/admin', ['as'=> 'image.add', 'uses' => 'diligents@dili' ]);

admin.blade.php

<form class="row login_form" action="/admin" method="post"  
novalidate="novalidate" enctype="multipart/form-data">
{{csrf_field()}}
<div class="col-md-12 form-group">

<input type="text" class="form-control" id="job" name="jobtype" 
 placeholder="Job " onfocus="this.placeholder = 
 ''"onblur="this.placeholder = 'Job'">
</div>
<div class="col-md-12 form-group">
<input type="text" class="form-control" id="jobC" name="jobC" placeholder="Job Catagary" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Job Catagary'">
    </div>



<div class="col-md-12 form-group">
<textarea class="form-control" name="details" id="details" rows="4" placeholder="Details" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Details'"></textarea>
</div>

     <div>
<div class="col-md-12 form-group">
<p align="left">Upload Image</p>
<input type="file" name="image" id="image">
</div>

<div class="col-md-12 form-group col-xl-12">
<button type="submit" value="submit" class="primary-btn">Submit</button>

</div>
<div class="col-md-12 form-group col-xl-12">
<button type="submit" value="submit" class="primary-btn">Update</button>
</div></form>

这是我收到的错误消息:

  

getClientOriginalName()上调用成员函数string

1 个答案:

答案 0 :(得分:1)

尝试此代码。

   $image = $request->file('image');

   $path = $imag->getClientOriginalName();

   dd($path);

通过dd(),您可以找到图片名称。 我希望错误会被消除。