Ste*_*and 2 php file-upload laravel
所以我在我的项目中创建了一个文件上传功能.
但是,当我尝试它时,我收到此错误: Call to a member function isValid() on string
我的上传功能代码:
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');
}
}
}
Run Code Online (Sandbox Code Playgroud)
从下面第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>
Run Code Online (Sandbox Code Playgroud)
您必须询问isValid()文件,而不是文件名.这就是你得到错误的原因.您可以通过$request->file()或通过 Input::file()以下方式获取文件:
else{
if( $request->file('profielfoto')->isValid()){ //<- gives error
Run Code Online (Sandbox Code Playgroud)
您的表单还应包含正确的enctype来发送文件:
<form enctype="multipart/form-data">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5520 次 |
| 最近记录: |