在非对象上调用成员函数getClientOriginalName()

问题描述 投票:9回答:5

我正在尝试制作图片上传器,但它总是给我这个错误

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');
    }
}

这是上传表格

<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>

我的代码出了什么问题?

php object upload laravel laravel-4
5个回答
22
投票

你错过了表单标记中的enctype属性。

要么这样做

<form role="form" action="{{URL::to('user/poster/upload_process')}}" method="post" enctype="multipart/form-data">
...
</form>

或这个...

{{ Form::open(array('url' => 'user/poster/upload_process', 'files' => true, 'method' => 'post')) }}
// ...
{{ Form::close() }}

1
投票

这些代码是正确的,但您没有检查Input::file('image')的返回值。我认为返回值可能不是正确的对象或您的类输入没有公共函数名称是getClientOriginalName

码:

$file = Input::file('image');
var_dump($file); // if return a correct object. you will check your class Input.

祝好运。


0
投票

请检查您的表单'files'=> true {!! Form :: open(['route'=> ['Please Type Url'],'class'=>'form-horizo​​ntal','files'=> true])!!}


0
投票

这只是因为你忘记在enctype="multipart/form-data"标签中写<form>

当您忘记这一点时会发生此错误:

<form class="form form-horizontal" method="post" action="{{ route('articles.store') }}" enctype="multipart/form-data">

0
投票
{!! Form::open(array('url' => '/xyz','files' => true)) !!}
{!! Form::close() !!}
© www.soinside.com 2019 - 2024. All rights reserved.