php - 如何验证来自表单验证库codeigniter的图像

标签 php codeigniter validation

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

Controller

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;
        }
    }


}

如有任何建议和帮助,我们将不胜感激。

最佳答案

它在表单验证中不起作用,试试吧,

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

关于php - 如何验证来自表单验证库codeigniter的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18612729/

相关文章:

linq-to-sql - 根据相关数据库列验证字符串长度

asp.net - 2010 年在 ASP.NET 中进行客户端/服务器验证的最佳方法是什么?

php - 数据库 : String Json vs new column

PHP:如何使用 SimpleXMLElement 处理 <![CDATA[?

php - 如何包含或引导 paypal-core-sdk php

php - Codeigniter 多数据库连接中遇到错误

php - Chrome 中页面刷新时 MySQL 查询运行两次

PHP 不会插入聊天消息

php - 如何在codeigniter中将包含html标签的csv文件导入MYSQL

验证错误 : PayPal IPN response from a different email address