在 laravel 4 上验证上传图片
validate uploading image on laravel 4
我是 Laravel 的新手。我有一个带有文件上传功能的表单。如何验证图像文件? .当我执行我的代码时,图像正在上传,但 URL 没有保存到 MySQL 数据库,并且它在表单上显示如下验证错误。
The thumbnail must be an image.
The large image must be an image.
这是我的输入和验证码。
{{ Form::open(array('url' => 'admin/templates/save', 'files' => true, 'method' => 'post')) }}
@if($errors->has())
@foreach($errors->all() as $error)
<div data-alert class="alert-box warning round">
{{$error}}
<a href="#" class="close">×</a>
</div>
@endforeach
@endif
<div class="row">
<div class="small-5 large-5 column">
{{ Form::label('title','Title:') }}
{{ Form::text('title',Input::old('title')) }}
</div>
</div>
<div class="row">
<div class="small-7 large-7 column">
{{ Form::label('description','Description:') }}
{{ Form::textarea('description',Input::old('description'),['rows'=>5]) }}
</div>
</div>
<div class="row">
<div class="small-7 large-7 column">
{{ Form::label('detailed_description','Detailed description:') }}
{{ Form::textarea('detailed_description',Input::old('detailed_description'),['rows'=>5]) }}
</div>
</div>
<div class="row">
<div class="small-5 large-5 column">
{{ Form::label('thumbnail','Choose thumbnail image:') }}
{{ Form::file('thumbnail') }}
</div>
</div>
<div class="row">
<div class="small-5 large-5 column">
{{ Form::label('large_image','Choose large image:') }}
{{ Form::file('large_image') }}
</div>
</div>
<div class="row">
<div class="small-5 large-5 column">
{{ Form::label('targeturl','Target URL:') }}
{{ Form::text('targeturl',Input::old('Target URL')) }}
</div>
</div>
{{ Form::submit('Save',['class'=>'button tiny radius']) }}
{{ Form::close() }}
控制器代码
<?php
class TemplateController extends BaseController
{
public function newTemplate()
{
$this->layout->title = 'New Template';
$this->layout->main = View::make('dash')->nest('content', 'templates.new');
}
public function saveTemplate() {
//TODO - Validation
$destinationPath = '';
$filename = '';
$destinationThumb = '';
$thumbname = '';
$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);
$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()) {
$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();
}
}
请帮帮我。
将您的上传代码移到验证通行证中
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();
}
我是 Laravel 的新手。我有一个带有文件上传功能的表单。如何验证图像文件? .当我执行我的代码时,图像正在上传,但 URL 没有保存到 MySQL 数据库,并且它在表单上显示如下验证错误。
The thumbnail must be an image. The large image must be an image.
这是我的输入和验证码。
{{ Form::open(array('url' => 'admin/templates/save', 'files' => true, 'method' => 'post')) }}
@if($errors->has())
@foreach($errors->all() as $error)
<div data-alert class="alert-box warning round">
{{$error}}
<a href="#" class="close">×</a>
</div>
@endforeach
@endif
<div class="row">
<div class="small-5 large-5 column">
{{ Form::label('title','Title:') }}
{{ Form::text('title',Input::old('title')) }}
</div>
</div>
<div class="row">
<div class="small-7 large-7 column">
{{ Form::label('description','Description:') }}
{{ Form::textarea('description',Input::old('description'),['rows'=>5]) }}
</div>
</div>
<div class="row">
<div class="small-7 large-7 column">
{{ Form::label('detailed_description','Detailed description:') }}
{{ Form::textarea('detailed_description',Input::old('detailed_description'),['rows'=>5]) }}
</div>
</div>
<div class="row">
<div class="small-5 large-5 column">
{{ Form::label('thumbnail','Choose thumbnail image:') }}
{{ Form::file('thumbnail') }}
</div>
</div>
<div class="row">
<div class="small-5 large-5 column">
{{ Form::label('large_image','Choose large image:') }}
{{ Form::file('large_image') }}
</div>
</div>
<div class="row">
<div class="small-5 large-5 column">
{{ Form::label('targeturl','Target URL:') }}
{{ Form::text('targeturl',Input::old('Target URL')) }}
</div>
</div>
{{ Form::submit('Save',['class'=>'button tiny radius']) }}
{{ Form::close() }}
控制器代码
<?php
class TemplateController extends BaseController
{
public function newTemplate()
{
$this->layout->title = 'New Template';
$this->layout->main = View::make('dash')->nest('content', 'templates.new');
}
public function saveTemplate() {
//TODO - Validation
$destinationPath = '';
$filename = '';
$destinationThumb = '';
$thumbname = '';
$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);
$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()) {
$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();
}
}
请帮帮我。
将您的上传代码移到验证通行证中
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();
}