繁体   English   中英

在laravel 4上验证上传图片

[英]validate uploading image on laravel 4

我是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() }} 

控制器代码

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM