laravel 调用非对象上的成员函数 getClientOriginalExtension() 即使 'file' => true

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

Call to a member function getClientOriginalExtension() on a non-object even with 'file' => true

phpformsfile-uploadlaravellaravel-4

提问by retrograde

I am getting a "Call to a member function getClientOriginalExtension() on a non-object", even though I have taken advice I have found to eliminate it, including adding 'files' => true to my {{ Form::open }}.

我收到了“对非对象的成员函数 getClientOriginalExtension() 的调用”,尽管我已经采纳了我发现要消除它的建议,包括将“文件”=> true 添加到我的 {{ Form::open } }.

Any idea what step I am getting wrong?

知道我哪一步出错了吗?

EDIThere is my routing

编辑这里是我的路由

Route::resource('/contractors', 'ContractorController');
Route::controller('/contractors', 'ContractorController');
Route::post('/contractors/portfolio/{$id}', 'ContractorController@post_Portfolio');

Here is my controller:

这是我的控制器:

public function post_Portfolio($id)
    {

    $contractor = Contractor::find($id);
    $input = Input::all();
    $rules = array(
      'fileToUpload' => 'image|max:3000',
  );

  $validation = Validator::make($input, $rules);

  if ($validation->fails())
  {
    return Response::make($validation->errors->first(), 400);
  }
    $file = Input::file('filesToUpload');
    $destinationPath = 'uploads/portfolio';
    $extension = $file->getClientOriginalExtension(); 
    $filename = str_random(12).".{$extension}";
    $upload_success = Input::file('filesToUpload')->move($destinationPath, $filename);

    if( $upload_success ) { 

        //create a new portfolio and transfer the inputs to the db

        $portfolio = new Portfolio;
        $contractor = Contractor::find($id);
        $portfolio->title       = Input::get('title');
        $portfolio->description     = Input::get('projectDetails');
        $portfolio->contractor_id   = Input::get('contractor_id');
        $portfolio->save();

        $photo = new ContractorPhoto;
        $photo->portfolio_id = $portfolio->id;
        $photo->path = $destinationPath ."/". $filename;
        $photo->save();

        // redirect
        Session::flash('message', 'Successfully updated profile!');
        return Redirect::to('contractors/');

    } else {
        Session::flash('message', 'Houston we have a problem!');
        return Redirect::to('contractors/'); 
    }



}

And here is my view:

这是我的观点:

<div class="span8 bdb bdb-larger">
        {{ Form::open(['url' => 'contractors/portfolio/{$id}','files' => true, 'method' => 'post', $contractor->id]) }}
            <input type="hidden" name="contractor_id"  value="{{$contractor->id}}">
        <div class="row">
            <div class="span6">
                <label for="title">Project Name<span class="required">*</span></label>
                <input type="text" name="title" id="title">
                <input type="hidden" name="contractor_id"  value="{{$contractor->id}}">
            </div>
        </div>


        <div class="row">
            <div class="span6">
                <label for="projectDetails">Project Details <span class="required">*</span></label>
                <textarea name="projectDetails" class="row-fluid" id="projectDetails"></textarea>
            </div>
        </div>
            <div class="row">
            <div class="span6">
                <div> 
                <label for="filesToUpload">Cover Photo</label>
                <input class="filestyle" type="file" name="filesToUpload[]" id="filesToUpload" multiple="multiple" />
                <script>$(":file").filestyle({classInput: "input-small"});</script>
                </div> 
            </div>
        </div>

        <div class="row">
            <div class="span6">
                <br />
                <div> 
                <label for="photos">Additional Photos</label>
                <input class="filestyle" type="file" name="filesToUpload[]" id="filesToUpload" multiple="multiple" />
                <script>$(":file").filestyle({classInput: "input-small"});</script>

                </div> 
            </div>
        </div>
        <div class="row">
            <div class="span6">
                <output id="filesInfo"></output>
            </div>
        </div>

        <div class="row">
            <div class="span6">
                <br /><input type="submit" id="btn-porfolio" class="btn btn-primary btn-read-more" value="Submit">
            </div>
        </div>
    </div>
    {{form::close()}}

</div> 

{{ HTML::script('assets/js/scripts/portfolio.js') }}

{{ HTML::script('assets/js/scripts/portfolio.js') }}

回答by rustynailor

I've just been implementing dropzone with Laravel, and I ran across a similar issue - I think the problem may be that filesToUpload is actually an array, so whilst Input::hasFile will return true, you will only be able to perform $file->move() or $file->getClientOriginalExtension() on elements within that array.

我刚刚在 Laravel 中实现 dropzone,我遇到了类似的问题 - 我认为问题可能是 filesToUpload 实际上是一个数组,所以虽然 Input::hasFile 将返回 true,但您将只能执行 $file ->move() 或 $file->getClientOriginalExtension() 在该数组中的元素上。

So if you tried supplying an index:

因此,如果您尝试提供索引:

$file = Input::file('filesToUpload[0]');

The rest should work. I know this is an old thread, but it came up prominently when I was googling and I thought this might help someone with the same problem.

其余的应该工作。我知道这是一个旧线程,但是当我在谷歌搜索时它很突出,我认为这可能会帮助遇到同样问题的人。

回答by phoops

You need to check if the file was actually uploaded before trying to do operations with it. I guess the error you get is mostly when you do NOT upload the file, but still try to perform operations. Update your code as following:

在尝试对其进行操作之前,您需要检查该文件是否确实已上传。我猜你得到的错误主要是当你没有上传文件,但仍然尝试执行操作时。更新您的代码如下:

if (Input::hasFile('filesToUpload'))
{
    $file = Input::file('filesToUpload');
    $destinationPath = 'uploads/portfolio';
    $extension = $file->getClientOriginalExtension();
    $filename = str_random(12).".{$extension}";
    $upload_success = $file->move($destinationPath, $filename);
}

More info on file handling in laravel documentation: http://laravel.com/docs/requests#files

有关 laravel 文档中文件处理的更多信息:http://laravel.com/docs/requests#files