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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20040745/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:48:58  来源:igfitidea点击:

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

phpobjectuploadlaravellaravel-4

提问by Spadaboyz

I'm trying to make an image uploader, but it always give me this error

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

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

here is my code controller code

这是我的代码控制器代码

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

and here is the upload form

这是上传表格

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

what's wrong with my code?

我的代码有什么问题?

回答by Andreyco

You miss enctypeattribute in your form markup.

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

Either do this

要么这样做

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

or this...

或这个...

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

回答by user3003394

These code are right, but you didn't check values of returns of Input::file('image'). I think returns value may be is not a correct object or your class Input does not have a public function name is getClientOriginalName.

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

Code:

代码:

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

Good luck.

祝你好运。

回答by Pyae Sone Sone

Please Check Your Form 'files'=> true {!! Form::open(['route' => ['Please Type Url'], 'class' => 'form-horizontal' , 'files' => true]) !!}

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

回答by hamidreza ghanbari

This is just because you forget to write enctype="multipart/form-data"in <form>tag.

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

This error happen just when you forget this:

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

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

回答by Parth Desai

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

回答by Mehedi Abdullah

if you are using Laravel Collective than you can try this solution

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

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

{{ Form::close() }}

else if you are using html form tag than you have to put extra markdown for storing image data

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

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