Laravel图片无法上传

时间:2017-08-23 14:56:59

标签: image upload laravel-5.4

我正在使用Laravel 5.4应用程序,我尝试上传的图片没有移动到我的磁盘上。 你能帮帮我吗?

我的表格:

<form class="form-horizontal" role="form" method="POST" action="{{ route('adminPostNews') }}" accept-charset="UTF-8" enctype="multipart/form-data">

          {{ csrf_field() }}

          <div class="form-group{{ $errors->has('picture') ? ' has-error' : '' }}">
              <label for="picture" class="col-md-1 control-label">Photo</label>

              <div class="col-md-6">
                <input type="file" id="picture" name="picture">

                  @if ($errors->has('picture'))
                      <span class="help-block">
                          <strong>{{ $errors->first('picture') }}</strong>
                      </span>
                  @endif
              </div>
          </div>

          <div class="form-group">
              <div class="col-md-6 col-md-offset-4">
                  <button type="submit" class="btn btn-primary">
                      Ajouter
                  </button>
              </div>
          </div>
      </form>

控制器:

public function store_actualites(Request $request)
{

  $this->validate($request, [
  'picture' => 'required|mimes:jpeg']);

  if($request->file('picture'))
  {
    $file = $request->file('picture');
    $extension = $file->getClientOriginalExtension();
    Storage::disk('news')->put($file->getFilename().'.'.$extension,  File::get($file));
  }

}

我的磁盘没问题,Laravel提供了该文件的名称,但上传无效。

2 个答案:

答案 0 :(得分:2)

您没有正确传递文件名,请尝试以下操作:

//Save image
if ($request->hasFile('picture')) {
    $file = $request->file('picture');
    $fileName = $file->getClientOriginalName();
    Storage::disk('news')->put($fileName, File::get($file));
}

答案 1 :(得分:0)

好的,也许第一个答案是:服务器文件上传大小限制! 其次,这是我的新代码:

$file = $request->file('picture');
    $extension = $file->getClientOriginalExtension();
    Storage::disk('news')->put($file->getFilename().'.'.$extension,  file_get_contents($file));

我没有使用&#34; file_get_contents&#34;的其他内容。