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

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

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

phplaravelupload

提问by Jensej

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

在非对象上调用成员函数 getClientOriginalName() 是我的代码控制器代码

public function postSubtitle()
{
    //$video = Video::where('user_id', '=', Auth::id())->find(Input::all('id'));

    var_dump(Input::all());
    $file= Input::file('name');
    echo $file->getClientOriginalExtension();   
}

and here is the upload form

这是上传表格

{{ form_open({'url': 'video/subtitle', 'files': 'true'}) }}

                    {{ form_file('name', {class: 'form-control'} ) }}
                    {{ form_submit(trans('main.edit'), {class: 'btn btn-lg btn-success btn-block'}) }}
                    {{form_close()}}

what's wrong with my code?

我的代码有什么问题?

回答by Marwelln

If you want to handle file uploads, your formmust have enctype="multipart/form-data". (You might have that, but we don't know how your function form_openworks.)

如果你想处理文件上传,你form必须有enctype="multipart/form-data". (你可能有那个,但我们不知道你的函数是如何form_open工作的。)

If you have that but still get the same error it should mean that you haven't selected any file. If you don't upload a file, Input::file('name')will be nullinstead of an object.

如果你有那个但仍然得到同样的错误,这应该意味着你没有选择任何文件。如果你不上传文件,Input::file('name')null代替一个对象。

So what you should do is to first check if it's not null, and then continue to handle your file.

所以你应该做的是首先检查它是否不为空,然后继续处理你的文件。

$file = Input::file('name');
if ($file !== null) {
    echo $file->getClientOriginalExtension();  
}