Php 对非对象调用成员函数getClientOriginalName()
我正试图使一个图像上传,但它总是给我这个错误Php 对非对象调用成员函数getClientOriginalName(),php,object,upload,laravel,laravel-4,Php,Object,Upload,Laravel,Laravel 4,我正试图使一个图像上传,但它总是给我这个错误 Call to a member function getClientOriginalName() on a non-object 这是我的代码控制器代码 public function uploadImageProcess(){ $destinatonPath = ''; $filename = ''; $file = Input::file('image'); $destinationPath = publi
Call to a member function getClientOriginalName() on a non-object
这是我的代码控制器代码
public function uploadImageProcess(){
$destinatonPath = '';
$filename = '';
$file = Input::file('image');
$destinationPath = public_path().'/assets/images/';
$filename = str_random(6).'_'.$file->getClientOriginalName();
$uploadSuccess = $file->move($destinationPath, $filename);
if(Input::hasFile('image')){
$images = new Images;
$images->title = Input::get('title');
$images->path = '/assets/images/' . $filename;
$image->user_id = Auth::user()->id;
Session::flash('success_insert','<strong>Upload success</strong>');
return Redirect::to('user/dashboard');
}
}
public函数uploadImageProcess(){
$destinationpath='';
$filename='';
$file=Input::file('image');
$destinationPath=public_path()。/assets/images/;
$filename=str_random(6)。“'.$file->getClientOriginalName();
$uploadSuccess=$file->move($destinationPath,$filename);
if(输入::hasFile('image')){
$images=新图像;
$images->title=Input::get('title');
$images->path='/assets/images/'.$filename;
$image->user\u id=Auth::user()->id;
会话::flash('success_insert','上传成功);
返回重定向::到('user/dashboard');
}
}
这是上传表格
<form role="form" action="{{URL::to('user/poster/upload_process')}}" method="post">
<label>Judul Poster</label>
<input class="form-control" type="text" name="title">
<label>Poster</label>
<input class="" type="file" name="image"><br/>
<input class="btn btn-primary" type="submit" >
</form>
朱杜尔海报
海报
我的代码怎么了?这些代码是正确的,但是您没有检查
Input::file('image')
的返回值。我认为返回值可能不是正确的对象,或者您的类输入没有公共函数名为getClientOriginalName
代码:
祝你好运。在表单标记中缺少
enctype
属性
要么这样做
<form role="form" action="{{URL::to('user/poster/upload_process')}}" method="post" enctype="multipart/form-data">
...
</form>
请检查您的表格“文件”=>true
{!!Form::open(['route'=>['Please Type Url'],'class'=>'Form horizontal','files'=>true])!!这只是因为您忘记在
标记中写入enctype=“多部分/表单数据”
当您忘记以下内容时,会发生此错误:
<form class="form form-horizontal" method="post" action="{{ route('articles.store') }}" enctype="multipart/form-data">
如果您使用的是Laravel Collective,则可以尝试此解决方案
{{Form::open(数组('url'=>'user/poster/upload_process','files'=>true','method'=>'post'))}
{{Form::close()}}
否则,如果您使用的是html表单标记,则必须为存储图像数据添加额外的标记
谢谢,结果为空,因此,如何正确获取输入文件?您可能需要在代码段中添加解释
{{ Form::open(array('url' => 'user/poster/upload_process', 'files' => true, 'method' => 'post')) }}
// ...
{{ Form::close() }}
<form class="form form-horizontal" method="post" action="{{ route('articles.store') }}" enctype="multipart/form-data">
{!! Form::open(array('url' => '/xyz','files' => true)) !!}
{!! Form::close() !!}