即使使用'文件,也可以在非对象上调用成员函数getClientOriginalExtension()。 =>真正

时间:2014-04-05 23:34:34

标签: php forms file-upload laravel laravel-4

我得到一个"调用非对象"上的成员函数getClientOriginalExtension(),即使我已经找到了消除它的建议,包括添加'文件&#39 ; =>对我的{{Form :: open}}是真的。

知道我走错了哪一步?

编辑这是我的路由

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

这是我的控制器:

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



}

以下是我的观点:

<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(&#39; assets / js / scripts / portfolio.js&#39;)}}

2 个答案:

答案 0 :(得分:5)

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

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

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

其余的应该有效。我知道这是一个老线程,但是当我在谷歌上搜索时它显得突出,我认为这可能会帮助有同样问题的人。

答案 1 :(得分:2)

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

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

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