【问题标题】:how to validate image from form validation library codeigniter如何从表单验证库codeigniter验证图像
【发布时间】:2013-09-04 11:34:38
【问题描述】:

我正在尝试在 codeigniter 中验证我的表单验证扩展类中的图像,但它不起作用

控制器

public function __construct() {
    parent::__construct();
    $this->load->library('form_validation');
}

public function add_photo() { 

 $this->form_validation->set_rules('pic','Photo','validate_image['.$_FILES['pic'].']');


  if($this->form_validation->run()) {
   //some further coding
  }
}

在我的图书馆里

class MY_Form_validation extends CI_Form_validation {

        public function __construct() {
        parent::__construct();
        $this->_CI = &get_instance();
    }

public function validate_image($image = NULL) {
        print_r($image);
        $file_name      =   $image['name'];

        $allowed_ext    =   array('jpg', 'jpeg', 'png', 'gif', 'bmp');
        $ext                =   strtolower(end(explode('.', $file_name)));

        $allowed_file_types =   array('image/jpeg','image/jpg','image/gif','image/png');
        $file_type              =   $image['type'];

        if(!in_array($ext, $allowed_ext) && !in_array($file_type, $allowed_file_types)) {
            $this->_CI->form_validation->set_message('validate_image', 'This file type is not allowed');
            return false;
        }

        if($image['size'] > 2097152) {
            $this->_CI->form_validation->set_message('validate_image', 'File size Exeeds');
            return false;
        } else {
            return true;
        }
    }


}

任何建议和帮助将不胜感激。

【问题讨论】:

  • 获取扩展更简单的方法:$ext = pathinfo($file_name, PATHINFO_EXTENSION);

标签: php codeigniter validation


【解决方案1】:

它在form validation 中不起作用,试试吧,

function validate_image(){
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']    = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

    $this->upload->initialize($config);
    if (!$this->upload->do_upload('userfile'))
    {
        $this->validation->set_message('userfile',$this->upload->display_errors());
        return false;
    } else {
        return true;
    }
}  

参考this

【讨论】:

  • rohan 什么是用户文件
  • userfile 是您的namefile element
  • 是的,我已经看到了,但我不知道要使用什么文件上传类,因为我只想验证图像并想从库中设置消息,就像我正在尝试做的那样。
  • 嗨@RohanKumar,我的问题是,如果我们必须上传多张图片怎么办。如果我在图像之间上传任何非图像文件,那么 codeigniter 会给出完美的错误,但它也会上传一些文件。如何防止这种情况。
  • 在这种情况下,您需要进行 validate_image_callback ,其中您必须为每个图像扩展运行一个循环,如果图像具有无效扩展,那么它将返回错误(FALSE),因此表单验证将返回 false。
【解决方案2】:

你可以这样做: 控制器:

public function add_photo() { 
    $this->form_validation->set_rules('product_image','Product Image','callback_validate_image');
    if($this->form_validation->run()==FALSE){
        $this->add($category_id);    
    }else{
        //your logic here
    }
}

回调验证方法:

public function validate_image() {
    $config = array(
        'allowed_types'=>'jpg|png',
        'upload_path'=> realpath(APPPATH . '../assets/img'),
        'max_size'=>1
        );
    $this->load->library('upload', $config);
    if (!$this->upload->do_upload('product_image'))
    {
        $this->form_validation->set_message('validate_image',$this->upload->display_errors());
        return false;
    } else {
        return true;
    }
}

APPPATH 将为您提供 codeigniter 应用程序文件夹的路径。
realpath() 将为您提供不带 ../ 的实际路径

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 2016-09-05
    • 2021-10-09
    • 1970-01-01
    相关资源
    最近更新 更多