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

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

Laravel 5.1 File Upload isValid() on string?

phplaravelfile-upload

提问by Stefano Groenland

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

但是,当我尝试时,我收到此错误: 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!

从下往下第 4 行的刀片视图中的表单是输入的位置!

<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>

回答by Amarnasan

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():

您必须询问isValid()文件,而不是文件名。这就是你得到错误的原因。您可以通过$request->file()或通过 Input::file()以下方式获取文件:

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

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

此外,您的表单应包含正确的 enctype 以发送文件:

<form enctype="multipart/form-data">

回答by xuwenzhi

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"