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

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">
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongshan4518 2015-12-23 09:40

    I think you should use as this.

    $file = $request -> file('Filedata');
    if (!$file -> isValid()) {
         echo Protocol::ajaxModel('JSEND_ERROR', 'not an valid file.');
         return;
    }
    

    Add attribute on

    enctype="multipart/form-data"
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题