【问题标题】:Call to a member function getClientOriginalName() on a non-object在非对象上调用成员函数 getClientOriginalName()
【发布时间】:2013-12-01 04:30:07
【问题描述】:

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

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      代码:

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

      祝你好运。

      【讨论】:

      • 谢谢,结果为 NULL 那么如何才能正确获取输入文件?
      【解决方案3】:

      这只是因为你忘记在&lt;form&gt;标签中写enctype="multipart/form-data"

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

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:
          {!! Form::open(array('url' => '/xyz','files' => true)) !!}
          {!! Form::close() !!}
          

          【讨论】:

          • 您可能需要在您的代码 sn-p 中添加解释
          【解决方案6】:

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

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

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-11-20
            • 1970-01-01
            • 2015-02-11
            • 2018-09-29
            • 2018-04-03
            • 1970-01-01
            相关资源
            最近更新 更多