Hey folks,
I am back with some more codeigniter helper functions that may ease up with your development. You can merge these functions with my Part-1 post and make a single helper file. All you have to do then is load and fire.😜
/* Checking if the field exists in specified table of database
Response => returns true if not found otherwise false
*Params*
$id : value which you want to check
$tableField : name of the field in table which you want to compare
$tableName : name of table from which you want to check
*/
function checkIfExists($id, $tableField, $tableName){
$CI = & get_instance();
$res = $CI->db->select($tableField)
->where($tableField,$id)
->get($tableName)
->row_array();
return empty($res) ? false : true ;
}
/* Email helper
Response => returns true if send successfully otherwise false
*Params*
$data['from'] : from email address
$data['from_name'] : From name displayed in email
$data['to'] : to email address
$data['cc'] : to cc email address
$data['bcc'] : to bcc email address
$data['subject'] : subject of email
$data['message'] : content of email
*/
function sendEmail($data){
$CI = & get_instance();
$CI->load->library('email');
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.mailtrap.io',
'smtp_port' => 2525,
'smtp_user' => '*************',
'smtp_pass' => '*************',
'crlf' => "\r\n",
'newline' => "\r\n",
'mailtype' => 'html'
);
$CI->email->initialize($config);
$CI->email->from($data['from'], $data['from_name']);
$CI->email->to($data['to']);
$CI->email->cc($data['cc']);
$CI->email->bcc($data['bcc']);
$CI->email->subject($data['subject']);
$CI->email->message($data['message']);
$CI->email->send();
return ($CI->email->send()) ? TRUE : FALSE ;
}
/* Get Ip Address
Response => the ip address of the system
*/
function fetchIpAddress(){
return $ip = getenv('HTTP_CLIENT_IP')?:
getenv('HTTP_X_FORWARDED_FOR')?:
getenv('HTTP_X_FORWARDED')?:
getenv('HTTP_FORWARDED_FOR')?:
getenv('HTTP_FORWARDED')?:
getenv('REMOTE_ADDR');
}
/* Upload file
Response => the details of file uploaded
*Params*
$data['file'] : file eg $_FILES['testfile']
$data['inputFileName'] : input file tag name in form field
$data['path'] : path where you want to store image
$data['allowedExtension'] : name of extensions you want to upload '|' seperated eg 'jpg|png'
$data['maxSize'] : maximum size of file you want to upload
$data['maxWidth'] : maximum width of file you want to upload
$data['maxHeight'] : maximum height of file you want to upload
*/
function uploadFile($data){
$CI = & get_instance();
$filename= $data['file']["name"];
$file_ext = pathinfo($filename,PATHINFO_EXTENSION);
$config['file_name'] = md5(uniqid(rand(), true)).'.'.$file_ext;
$config['upload_path'] = $data['path']; //'assets/uploads/food_images';
$config['allowed_types'] = $data['allowedExtension']; //'jpg|png';
$config['max_size'] = $data['maxSize']; //5000;
$config['max_width'] = $data['maxWidth']; //1500;
$config['max_height'] = $data['maxHeight']; //1500;
$CI->load->library('upload', $config);
if (!$CI->upload->do_upload($data['inputFileName'])) {
$response = array('error' => $CI->upload->display_errors());
return $response;
}
else{
$response = $CI->upload->data();
return $response;
}
}
/*upload file and generate thumbnail
Response => The details of file and thumbnail uploaded
*Params*
$data['file'] : file eg $_FILES['testfile']
$data['inputFileName'] : input file tag name in form field
$data['filepath'] : path where you want to store the image
$data['thumbPath'] : path where you want to store the thumbnail
$data['allowedExtension'] : name of extensions you want to upload '|' seperated eg 'jpg|png'
$data['maxSize'] : maximum size of file you want to upload
$data['maxFileWidth'] : maximum width of file you want to upload
$data['maxFileHeight'] : maximum height of file you want to upload
$data['thumbailWidth'] : maximum height of file you want to upload
$data['thumbnailHeight'] : maximum height of file you want to upload
*/
// function fileUploadAndCreateThumb($file, $inputFileName, $filepath, $thumbPath, $allowedExtension, $maxSize, $maxFileWidth, $maxFileHeight, $thumbailWidth, $thumbnailHeight){
function fileUploadAndCreateThumb($data){
$CI = & get_instance();
$filename= $data['file']['name'];
$file_ext = pathinfo($filename,PATHINFO_EXTENSION);
$config['file_name'] = md5(uniqid(rand(), true)).'.'.$file_ext;
$config['upload_path'] = $data['filepath'];
$config['allowed_types'] = $data['allowedExtension'];
$config['max_size'] = $data['maxSize'];
$config['max_width'] = $data['maxFileWidth'];
$config['max_height'] = $data['maxFileHeight'];
$CI->load->library('upload', $config);
if (!$CI->upload->do_upload($data['inputFileName'])) {
$response = array('error' => $CI->upload->display_errors());
return $response;
}
else{
$fileData = $CI->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = $data['filepath'].'/'.$fileData['file_name'];
$config['new_image'] = $data['thumbPath'];
$config['maintain_ratio'] = TRUE;
$config['width'] = $data['thumbailWidth'];
$config['height'] = $data['thumbnailHeight'];
$CI->load->library('image_lib', $config);
if($CI->image_lib->resize()){
$response['fileName'] = $CI->image_lib->source_image;
}else{
$response = array('error' => $CI->image_lib->display_errors());
}
return $response;
}
}
?>
Any suggestions to improve this post will be highly appreciated.
Thanks
Top comments (0)