Php 验证在laravel 4上上载图像
我是拉威尔的新手。我有一个带有文件上传功能的表单。如何验证图像文件。当我执行代码时,图像被上传,但URL并没有保存到MySQL数据库,它在表单上显示验证错误,如下所示 缩略图必须是图像。 大图像必须是一个图像 这是我的输入和验证代码Php 验证在laravel 4上上载图像,php,mysql,laravel-4,laravel-validation,Php,Mysql,Laravel 4,Laravel Validation,我是拉威尔的新手。我有一个带有文件上传功能的表单。如何验证图像文件。当我执行代码时,图像被上传,但URL并没有保存到MySQL数据库,它在表单上显示验证错误,如下所示 缩略图必须是图像。 大图像必须是一个图像 这是我的输入和验证代码 {{Form::open(数组('url'=>'admin/templates/save','files'=>true,'method'=>'post'))} @如果($errors->has()) @foreach($errors->all()作为$error)
{{Form::open(数组('url'=>'admin/templates/save','files'=>true,'method'=>'post'))}
@如果($errors->has())
@foreach($errors->all()作为$error)
{{$error}}
@endforeach
@恩迪夫
{{Form::label('title','title:')}
{{Form::text('title',Input::old('title'))}
{{Form::label('description','description:')}
{{Form::textarea('description',Input::old('description'),['rows'=>5])}
{{Form::label('detailed_description','detailed description:')}
{{Form::textarea('detailed_description',Input::old('detailed_description'),['rows'=>5])}
{{Form::label('thumbnail','Choose thumbnail image:')}
{{Form::file('thumbnail')}
{{Form::label('large_image','Choose large image:')}
{{Form::file('large_image')}
{{Form::label('targeturl','targeturl:')}
{{Form::text('targeturl',Input::old('targeturl'))}
{{Form::submit('Save',['class'=>'button tiny radius'])}
{{Form::close()}
将上载代码移到验证过程中
public function saveTemplate() {
//TODO - Validation
$rules = [
'title' => 'required',
'description' => 'required',
'detailed_description' => 'required',
'targeturl' => 'required',
'thumbnail' => 'required|image',
'large_image' => 'required|image'
];
$validator = Validator::make(Input::all(), $rules);
if ($validator->passes()) {
$thumb = Input::file('thumbnail');
$destinationThumb = public_path().'/thumb/';
$thumbname = str_random(6) . '_' . $thumb->getClientOriginalName();
$uploadSuccess = $thumb->move($destinationThumb, $thumbname);
$file = Input::file('large_image');
$destinationPath = public_path().'/img/';
$filename = str_random(6) . '_' . $file->getClientOriginalName();
$uploadSuccess = $file->move($destinationPath, $filename);
$template = new Template();
$template->title = Input::get('title');
$template->description = Input::get('description');
$template->thunbnailURL = $destinationThumb . $thumbname;
$template->detailedDescription = Input::get('detailed_description');
$template->targetURL = Input::get('targeturl');
$template->detailImageURL = $destinationPath . $filename;
//$user->created_at = DB::raw('NOW()');
//$user->updated_at = DB::raw('NOW()');
$template->save();
return Redirect::back()->with('success', 'Template added!');
} else
return Redirect::back()->withErrors($validator)->withInput();
}
非常感谢@Sushant Aryal您能帮我解决问题吗>