laravel 5.2 |上传文件 - 在null上调用成员函数getClientOriginalName()

时间:2016-08-12 09:50:07

标签: php laravel-5.2

我试图上传个人资料图片,但我收到了错误"在null&#34上调用成员函数getClientOriginalName();

这是我的方法:

  $data = $request->input('fotodosen');
    $photo = $request->file('fotodosen')->getClientOriginalName();
    $destination = base_path() . '/public/uploads';
    $request->file('fotodosen')->move($destination, $photo);
    $data['fotodosen'] = $photo;
    Dosen::create($data);

创建:

 {!! Form::open(array('fotodosen'=>'create', 'method'=>'POST', 'files'=>true, 'url'=>'uploads')) !!}
             {!! Form::file('image') !!}
                   <div class="form-group">
                        <div class="col-md-6 col-md-offset-4">
                            <button type="submit" class="btn btn-primary">
                                <i class="fa fa-btn fa-user"></i> Register
                            </button>
                             {!! Form::close() !!}

已编辑方法:

 $photo = $request->file('fotodosen')->getClientOriginalName($photo);

仍然有错误。我错过了什么?

更新:

public function store(CreateDosenRequest $request)

{



    $user = User::create([
        'name' => $request->input('name'),
        'username' => $request->input('username'),
        'email' => $request->input('email'),
        'password' => $request->input('password'),
        'admin' => $request->input('admin'),
     ]);

      $dosen = Dosen::create([
        'iddosen' => $request->input('iddosen'),
        'nipy' => $request->input('nipy'),
        'namadosen' => $user->name,
        'user_id' => $user->id,
        'alamatdosen' => $request->input('alamatdosen'),
        'notelpdosen' => $request->input('notelpdosen'),
        'tempatlahirdosen' => $request->input('tempatlahirdosen'),
        'tanggallahirdosen' => $request->input('tanggallahirdosen'),
        'agamadosen' => $request->input('agamadosen'),

    ]);
        if ($request->hasFile('image')) {
            $data = $request->input('image');
            $photo = $request->file('image')->getClientOriginalName();
            $destination = public_path() . '/uploads/';
            $request->file('image')->move($destination, $photo);
            $data['fotodosen'] = $photo;
            Dosen::create($data);
 }

4 个答案:

答案 0 :(得分:5)

您的文件名为image,请尝试使用image代替fotodosen

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

完整代码

$data = $request->input('image');
$photo = $request->file('image')->getClientOriginalName();
$destination = base_path() . '/public/uploads';
$request->file('image')->move($destination, $photo);

您可以检查文件,

if ($request->hasFile('image')) {
    // your code here
}

来自Http Requests和文章file upload in laravel 5

答案 1 :(得分:1)

我遇到了同样的问题,所以我使用html表单属性enctype="multipart/form-data"

解决了这个问题

实施例

<form name="le_form" action="/ft" method="POST" enctype="multipart/form-data">

有关详细信息,请参阅

答案 2 :(得分:0)

对我来说,我发现我没有在表单上包含enctype'=>'multipart/form-data。当我这样做时,它最终解决了问题。

答案 3 :(得分:0)

如果您要上传文件或表单中有文件字段,请执行此操作

 {{  Form::open(array('url' => 'dashboard/new_job', 'enctype'=>'multipart/form-data')) }}

OR

<form  action="/ft" method="POST" enctype="multipart/form-data">