我正在尝试在 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/