小编MOH*_*HIR的帖子

如何从表单验证库Codeigniter验证图像

我正在尝试从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
  }
}
Run Code Online (Sandbox Code Playgroud)

在我的图书馆

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'] …
Run Code Online (Sandbox Code Playgroud)

php validation codeigniter

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

codeigniter ×1

php ×1

validation ×1