Skip to content

Instantly share code, notes, and snippets.

@aguileraq
Created September 10, 2019 01:37
Show Gist options
  • Save aguileraq/c7d65f938ac577e362765be2b5fc12cb to your computer and use it in GitHub Desktop.
Save aguileraq/c7d65f938ac577e362765be2b5fc12cb to your computer and use it in GitHub Desktop.
<?php
class MY_Form_validation extends CI_Form_validation{
public $ci;
public function __construct($config = array()){
parent::__construct($config);
$this->ci =& get_instance();
}
function edit_unique($value, $params) {
$CI =& get_instance();
$CI->load->database();
$CI->form_validation->set_message('edit_unique', "Sorry, that %s is already being used.");
list($table, $field, $current_id) = explode(".", $params);
//members.registration_number.$id
$query = $CI->db->select()->from($table)->where($field, $value)->limit(1)->get();
if ($query->row() && $query->row()->id != $current_id){
return FALSE;
}
}
function validate_image($field){
$CI =& get_instance();
$check = TRUE;
if ((!isset($_FILES[$field])) || $_FILES[$field]['size'] == 0) {
$CI->form_validation->set_message('validate_image', 'The {field} field is required');
$check = FALSE;
}else if (isset($_FILES[$field]) && $_FILES[$field]['size'] != 0) {
$allowedExts = array("gif", "jpeg", "jpg", "png", "JPG", "JPEG", "GIF", "PNG");
$allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
$extension = pathinfo($_FILES[$field]["name"], PATHINFO_EXTENSION);
$detectedType = exif_imagetype($_FILES[$field]['tmp_name']);
$type = $_FILES[$field]['type'];
if (!in_array($detectedType, $allowedTypes)) {
$CI->form_validation->set_message('validate_image', 'Invalid Image Content!');
$check = FALSE;
}
if(filesize($_FILES[$field]['tmp_name']) > 2000000) {
$CI->form_validation->set_message('validate_image', 'The Image file size shoud not exceed 20MB!');
$check = FALSE;
}
if(!in_array($extension, $allowedExts)) {
$CI->form_validation->set_message('validate_image', "Invalid file extension {$extension}");
$check = FALSE;
}
}
return $check;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment