繁体   English   中英

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

[英]Call to a member function getClientOriginalName() on a non-object

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

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>

我的代码有什么问题?

您错过了表单标记中的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() }}

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

代码:

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

祝你好运。

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

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

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

请检查您的表单'文件'=> true {!! Form::open(['route' => ['请输入 URL'], 'class' => 'form-horizo​​ntal', 'files' => true ]) !!}

{!! Form::open(array('url' => '/xyz','files' => true)) !!}
{!! Form::close() !!}

如果您使用的是 Laravel Collective,则可以尝试此解决方案

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

{{ Form::close() }}

否则,如果您使用 html 表单标签,则必须为存储图像数据添加额外的降价

<form class="form form-horizontal" method="post" action="{{ route('user/poster/upload_process') }}" enctype="multipart/form-data">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM