php laravel 5.2 | 上传文件 - 在 null 上调用成员函数 getClientOriginalName()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38914976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel 5.2 | upload file - Call to a member function getClientOriginalName() on null
提问by Ezra
i tried to upload profile picture pict but i got error "Call to a member function getClientOriginalName() on null"
我试图上传个人资料图片,但出现错误“调用成员函数 getClientOriginalName() on null”
this is my method :
这是我的方法:
$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);
create :
创建 :
{!! 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() !!}
already edit method to :
已经将方法编辑为:
$photo = $request->file('fotodosen')->getClientOriginalName($photo);
still got that error. what am i missing?
仍然有那个错误。我错过了什么?
UPDATE :
更新 :
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);
}
回答by Rohan Kumar
You have the file name as image
try to use image
instead of fotodosen
您有image
尝试使用的文件名image
而不是fotodosen
$photo = $request->file('image')->getClientOriginalName();
Full code
完整代码
$data = $request->input('image');
$photo = $request->file('image')->getClientOriginalName();
$destination = base_path() . '/public/uploads';
$request->file('image')->move($destination, $photo);
You can check for the file like,
您可以检查文件,例如,
if ($request->hasFile('image')) {
// your code here
}
From Http Requestsand an article file upload in laravel 5
来自Http 请求和laravel 5 中的文章文件上传
回答by Juan Caicedo
I had the same issue so I solved it using html form attribute enctype="multipart/form-data"
我遇到了同样的问题,所以我使用 html 表单属性解决了它 enctype="multipart/form-data"
Example
例子
<form name="le_form" action="/ft" method="POST" enctype="multipart/form-data">
For more information see
有关更多信息,请参阅
回答by Sterlingking
For me, what I found out is that i did not included the enctype'=>'multipart/form-data
on the form. When i did, it eventually solved the problem.
对我来说,我发现我没有enctype'=>'multipart/form-data
在表格中包含。当我这样做时,它最终解决了问题。
回答by Rashid Iqbal
just do this if you are uploading a file or have a file field inside your form
如果您要上传文件或表单中有文件字段,请执行此操作
{{ Form::open(array('url' => 'dashboard/new_job', 'enctype'=>'multipart/form-data')) }}
OR
或者
<form action="/ft" method="POST" enctype="multipart/form-data">