【问题标题】:Laravel4: upload a imageLaravel4:上传图片
【发布时间】:2016-11-05 11:53:30
【问题描述】:

我想通过表格上传照片。我已正确设置照片资源。带有:id、标题、标题、路径:字符串的表格照片。

这是要求用户上传图片的表单的一部分:

{{Form::open(array('route' => 'CrewRegisterPost', 'role'=>'form','files'=> true,'class' => 'form-horizontal','style' => 'margin-top:15px;'))}}
   <label class="col-md-4 control-label" for="license_image">Colour scanned copy of Licence *</label>  
    <div class="col-md-6">
       <input type="file" name="license_image" accept="image/*" required>   
           {{Form::showError('license_image')}}         
    </div>
{{Form::close()}}

这是我到现在为止做的简单的store方法:

public function createNewCrew($data){
        $user = $this->storeNewUserCommonFields($data,User::getTypeNumberFor('Crew'));

        $validator = new Saarang\Validators\FlightCrewValidator($data);

        if($validator->fails()){
            $this->messageBag->merge($validator->errors->getMessages());
        }

        if($user){
            $Crew = new Crew();
            $Crew->user_id = $user->getKey();
            $Crewlicense_image = $data['license_image'];
            $savepath = 'public/img/';
            $filename = time() . '-' .$Crewlicense_image;
            Input::file('license_image')->move($savepath, $filename);
            $Crew->save();
            return $user;
       }
}

但是当我运行它时出现以下错误:调用成员函数 move() 在非对象上

我怎样才能正确地把那个实现放在那里以检索文件并存储在位于 public/img 的文件夹中?

【问题讨论】:

  • 首先你应该做一个表单并将enctype属性设置为多部分表单数据,然后将你的输入放在上面的表单中。
  • 作为一个建议,dropzone 插件对我帮助很大。

标签: php laravel laravel-4


【解决方案1】:

尝试使用:-

move(base_path().savepath, $filename);

而不是

move($savepath, $filename);

因为不写base_path()方法就显示出这类错误。

希望这对你有用。

【讨论】:

    猜你喜欢
    • 2013-08-23
    • 2014-06-07
    • 2015-03-01
    • 2014-02-25
    • 2021-03-19
    • 2010-12-02
    • 2010-12-15
    相关资源
    最近更新 更多