在laravel 4上验证上传图片

时间:2015-01-15 04:41:40

标签: php mysql laravel-4 laravel-validation

我是Laravel的新手。我有一个带有文件上传功能的表单。如何验证图像文件? 。当我执行我的代码时,图像被上传但URL没有保存到MySQL数据库,它在表单上显示验证错误如下。

  

缩略图必须是图像。   大图像必须是图像。

这是我的输入和验证码。



{{ 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">&times;</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() }}
&#13;
&#13;
&#13;

控制器代码

&#13;
&#13;
<?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();
    	
    }
}
&#13;
&#13;
&#13;

请帮帮我。

1 个答案:

答案 0 :(得分:2)

将上传代码移到验证通行证中

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

}