douwen7603 2015-12-23 09:16
浏览 1016
已采纳

Laravel 5.1文件上传isValid()字符串?

So I am making a function for file uploading in my project.

however, when I try it I get this error : Call to a member function isValid() on string

My code for the upload function :

public function upload(Request $request){

    $file = array('profielfoto' => $request->input('profielfoto'));

    $rules = array('profielfoto' => 'required',);

    $validator = Validator::make($file,$rules);
    if($validator->fails()){
        return redirect('/profiel')->withInput()->withErrors($validator);
    }
    else{
        if($request->input('profielfoto')->isValid()){ //<- gives error
            $destinationPath = 'assets/uploads';
            $extension = $request->input('profielfoto')->getClientOriginalExtension();
            $fileName = rand(1111,9999).'.'.$extension;

            $request->input('profielfoto')->move($destinationPath,$fileName);



            Session::flash('alert-success', 'Foto uploaden gelukt');
            return redirect('/profiel');
        }
        else{
            Session::flash('alert-danger', 'Foto uploaden mislukt');
            return redirect('/profiel');
        }
    }
}

The form in the blade view on the 4th line from down below is the location for the input!

<form method="POST" action="/profiel/upload" files="true">
                      {!! csrf_field() !!}
                      <input type="hidden" name="_method" value="PUT">
                      <input type="hidden" class="form-control id2" id="id2"  name="id" value="{{$user->id}}">
                        <img src="assets/images/avatar.png" alt="gfxuser" class="img-circle center-block">
                        <div class="form-group center-block">
                        <label class="center-block text-center" for="fotoinput">Kies uw foto</label>
              <input class="center-block" type="file"  name="profielfoto" id="profielfoto">
                      </div>
                      <button type="submit" class="btn btn-success"><span class="fa fa-check" aria-hidden="true"></span> Verander foto</button>
                      </form>
  • 写回答

2条回答 默认 最新

  • dongqi8863 2015-12-23 09:38
    关注

    You must ask isValid() to a file, not to the name of the file. That's why you get the error. You can get the file through $request->file() or through Input::file() :

    else{
        if( $request->file('profielfoto')->isValid()){ //<- gives error
    

    Also your form should include the correct enctype to send files:

    <form enctype="multipart/form-data">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计