how would I insert "_thumb" before file extension - php

How would I insert "_thumb" before file extension ?
for example 123.jpg into 123_thumb.jpg
please answer my question, it's been 3 days i didn't find the right way.. thank you
this is my view :
<div class="row service-box margin-bottom-40">
<!-- carousel -->
<div class="col-md-4">
<div class="carousel slide">
<?php
$reports_slide = $this->m_dashboard->report_slide();
foreach ($reports_slide as $row) {
echo '
<img width="100%" src="' . base_url() . 'img/report/thumbs/' . $row->report_img . '" class="mySlides">
';
} ?>
<script>
var slideIndex = 0;
carousel();
function carousel() {
var i;
var x = document.getElementsByClassName("mySlides");
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
slideIndex++;
if (slideIndex > x.length) {slideIndex = 1}
x[slideIndex-1].style.display = "block";
setTimeout(carousel, 2000); // Change image every 2 seconds
}
</script>
</div>
</div>
and this is my controller :
public function add_report()
{
$field_img = "img";
$field_thumb = "thumb";
$field_pdf = "pdf";
// this is for form field 1 which is an image....
$config['upload_path'] = '../img/report/';
$config['allowed_types'] = 'gif|jpg|png|pdf';
$config['max_size'] = '1024';
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$this->load->view('includes/header');
$this->load->view('includes/menu');
if (isset($_POST['submit'])) {
if (isset($_FILES['img']['name']) && is_uploaded_file($_FILES['img']['tmp_name'])){
$config['file_name'] = '_image_'.$_POST['id_company'].'_'.$_POST['report_title'];
$this->upload->initialize($config);
$this->upload->do_upload($field_img);
$file_name = $this->upload->data();
$file_img = $file_name['file_name'];
$config = array(
'image_library' => 'gd2',
'source_image' => $file_name['full_path'],
'new_image' => '../img/report/thumbs/',
'create_thumb' => TRUE,
'maintain_ratio' => FALSE,
'width' => 827,
'height' => 1170,
);
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
}
}
}
and this is my Model :
public function report_slide()
{
//report
$this->db->select('a.*, b.category_name, c.co_abbreviated_name, d.country_name, e.language, f.report_type, g.year, h.type');
$this->db->from('rc_report a, rc_category b, rc_company c, rc_country d, rc_language e, rc_report_type f, rc_year g, rc_gri h');
$this->db->where('b.id_category = a.id_category');
$this->db->where('c.id_company = a.id_company');
$this->db->where('d.id_country = a.id_country');
$this->db->where('e.id_language = a.id_language');
$this->db->where('f.id_report_type = a.id_report_type');
$this->db->where('g.id_year = a.id_year');
$this->db->where('h.id_gri = a.id_gri');
$this->db->order_by('g.year','desc');
$this->db->limit(5);
$query_report = $this->db->get();
$result_array=$query_report->result();
return $result_array;
}

English is not my native language, sorry for that.
For set à new name for your thumb, you just change this config
'new_image' => '../img/report/thumbs/'
When you do this you get the lot of informations about file
$file_name = $this->upload->data();
If you like inside the Ci::Upload, you see the function data return this
public function data($index = NULL)
{
$data = array(
'file_name' => $this->file_name,
'file_type' => $this->file_type,
'file_path' => $this->upload_path,
'full_path' => $this->upload_path.$this->file_name,
'raw_name' => substr($this->file_name, 0, -strlen($this->file_ext)),
'orig_name' => $this->orig_name,
'client_name' => $this->client_name,
'file_ext' => $this->file_ext,
'file_size' => $this->file_size,
'is_image' => $this->is_image(),
'image_width' => $this->image_width,
'image_height' => $this->image_height,
'image_type' => $this->image_type,
'image_size_str' => $this->image_size_str,
);
if ( ! empty($index))
{
return isset($data[$index]) ? $data[$index] : NULL;
}
return $data;
}
In your case what interests you it's
'raw_name' => substr($this->file_name, 0, -strlen($this->file_ext)),
Which give :
$config = array(
'image_library' => 'gd2',
'source_image' => $file_name['full_path'],
'new_image' => "../img/report/thumbs/".$file_name['raw_name']."_thumb.".$file_name['file_ext'],
'create_thumb' => TRUE,
'maintain_ratio' => FALSE,
'width' => 827,
'height' => 1170,
);
PS: See this Codeigniter image resize() thumbnail name issue

i use this extension : http://www.matmoo.com/digital-dribble/codeigniter/image_moo/
It's more successful than Codeigniter's own library. Especially when I want to save thumbnails in multiple sizes.

Related

Codeigniter Uploading excel and Saving to database (Network Error (tcp_error))

Really need your support on this one.
This works on intranet not until we decided to upload it on public.
Im using Codeigniter + SQL Server + Apache
This how it should work:
upload the excel file, read the content, save to database and send an email & sms.
But:
Everytime I upload the excel file after 3 Mins it stops and got this error written on Developer Opt->Network Tab:
"Network Error (tcp_error)
A communication error occurred: ""
The Web Server may be down, too busy, or experiencing other problems preventing it from responding to requests. You may wish to try again at a later time."
Here's my code:
public function do_upload()
{
set_time_limit(0);
ignore_user_abort(1);
$accID = $this->input->get('acc');
$rmi = $this->input->get('rmi');
$spInst = $this->input->get('spInst');
$dateReq = '';
$dateReqq = $this->input->get('dateReq');
if($dateReqq==null or $dateReqq==''){
$dateReq = date('Y-m-d');
}else{
$dateReq = $this->input->get('dateReq');
}
$dateNow = date("F_d_Y__h_i_s__A");
$status = "";
$msg = "";
$file_element_name = 'file';
$file_name = '';
if ($status != "error")
{
$config['upload_path'] = 'c:/xampp/htdocs/eDR/assets/uploads/';
$config['allowed_types'] = 'xlsx';
$config['max_size'] = 60000;
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['file_name'] = uniqid('file')."_". $dateNow;
$this->load->library('upload', $config);
if (!$this->upload->do_upload($file_element_name))
{
$status = FALSE;
$msg = $this->upload->display_errors('', '');
}
else
{
$data = $this->upload->data();
//$file_id = $this->files_model->insert_file($data['file_name'], $_POST['title']);
//if($file_id)
//{
$file_name = $data['file_name'];
$file = "c:/xampp/htdocs/eDR/assets/uploads/" . $file_name;
$this->do_read($file,$accID,$rmi,$spInst,$dateReq);
$status = TRUE;
$msg = "File successfully uploaded";
//}
//else
// {
// unlink($data['full_path']);
// $status = "error";
// $msg = "Something went wrong when saving the file, please try again.";
// }
}
#unlink($_FILES[$file_element_name]);
}
echo json_encode(array('status' => $status, 'msg' => $msg, 'name' => $file_name));
}
public function do_read($file,$accID,$rmi,$spInst,$dateReq){
//load the excel library
$this->load->library('excel');
//read file from path
$objPHPExcel = PHPExcel_IOFactory::load($file);
//get only the Cell Collection
$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
//extract to a PHP readable array format
foreach ($cell_collection as $cell) {
$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
$row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
$data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
//header will/should be in row 1 only. of course this can be modified to suit your need.
//if ($row == 1) {
// $header[$row][$column] = $data_value;
//} else {
$arr_data[$row][$column] = $data_value;
//}
}
//send the data in an array format
//$data['header'] = $header;
$drCount = ($this->up->get_DRcount($accID));
$date = date("F d, Y h:i: s A");
$data['values'] = $arr_data;
$last = count($arr_data) - 1;
$totalQty = 0;
$abbr = $this->up->get_Abbr($accID);
$TATnAGING = '';
$dAdd ='';
$posReq='';
$reqD;
foreach ($arr_data as $i => $row)
{
if( empty($row['B']) or
empty($row['C']) or
empty($row['D']) or
empty($row['E']) or
empty($row['F']) or
empty($row['G']) or
empty($row['H']) or
empty($row['I']) or
empty($row['J']) or
empty($row['K'])
){
$msg = 'B = ' . $row['B'] . ' C = ' . $row['C'] . ' D = ' . $row['D'] . ' E = ' . $row['E'] . ' F = ' . $row['F'] . ' G = ' . $row['G'] . ' H = ' . $row['H'] . ' I = ' . $row['I']
. ' J = ' . $row['J'] . ' K = ' . $row['K'] ;
$status = FALSE;
echo json_encode(array('status' => $status, 'msg'=>$msg));
exit();
}
}
foreach ($arr_data as $i => $row)
{
try {
$reqDatee = date('F d, Y', strtotime($dateReq));
$start = new DateTime( $reqDatee );
$end = new DateTime(date("F d, Y"));
$oneday = new DateInterval("P1D");
$days = array();
$data1 = "7.5";
foreach(new DatePeriod($start, $oneday, $end->add($oneday)) as $day) {
$day_num = $day->format("N"); /* 'N' number days 1 (mon) to 7 (sun) */
if($day_num < 6) { /* weekday */
$days[$day->format("Y-m-d")] = $data1;
}
}
$TATnAGING = count($days). ' day/s';
$dAdd = trim(strtoupper($row['D']));
$posReq = trim(strtoupper($row['B']));
$reqD = $dateReq;
$isFirst = ($i == 0);
$isLast = ($i == $last);
$id = $this->session->userdata('username');
$totalQty += $row['G'];
} catch (Exception $e) {
$msg = 'Caught exception: '. $e->getMessage(). "\n";
$status = FALSE;
echo json_encode(array('status' => $status, 'error'=>$msg));
exit();
}
if($i>1){
$data1 = array();
try {
if(trim(strtoupper($row['B']))=='YES' or trim(strtoupper($row['B']))=='Y'){
$data1 = array(
"drRef" => $abbr . '2017' . sprintf("%07d", ($drCount + 1)),
"postReq" => trim(strtoupper($row['B'])),
"reqDate" => $dateReq,
"reqBy" => trim(strtoupper($row['C'])),
"deliveryAcc" => trim(strtoupper($row['D'])),
"matCode" => trim(strtoupper($row['E'])),
"matDescription" => trim(strtoupper($row['F'])),
"qty" => $row['G'],
"UOM" => trim(strtoupper($row['H'])),
"location" => trim(strtoupper($row['I'])),
"postRef" => '',
// "postDate" => date("F d, Y h:i:s A"),
// "postBy" => $this->session->userdata['name'],
"status" => 'PENDING FOR POSTING',
// "TAT" => trim(strtoupper($row['O'])),
// "AGING" => trim(strtoupper($row['P'])),
"userID" => $id,
'addedBy' => $this->session->userdata('name'),
'addedUsing' => gethostbyaddr($_SERVER['REMOTE_ADDR']),
'dateAdded' => $date,
'seqNo' => uniqid(),
'accID' => $accID,
'fromIMEI' => trim(strtoupper($row['J'])),
'toIMEI' => trim(strtoupper($row['K'])),
);
}else{
$data1 = array(
"drRef" => $abbr .'2017' . sprintf("%07d", ($drCount + 1)),
"postReq" => trim(strtoupper($row['B'])),
"reqDate" => $dateReq,
"reqBy" => trim(strtoupper($row['C'])),
"deliveryAcc" => trim(strtoupper($row['D'])),
"matCode" => trim(strtoupper($row['E'])),
"matDescription" => trim(strtoupper($row['F'])),
"qty" => $row['G'],
"UOM" => trim(strtoupper($row['H'])),
"location" => trim(strtoupper($row['I'])),
"postRef" => '',
"postDate" => date("F d, Y h:i:s A"),
"postBy" => $this->session->userdata['name'],
"status" => 'POSTED',
"TAT" => $TATnAGING,
"userID" => $id,
'addedBy' => $this->session->userdata('name'),
'addedUsing' => gethostbyaddr($_SERVER['REMOTE_ADDR']),
'dateAdded' => $date,
'seqNo' => uniqid(),
'accID' => $accID,
'fromIMEI' => trim(strtoupper($row['J'])),
'toIMEI' => trim(strtoupper($row['K'])),
);
}
} catch (Exception $e) {
$msg = 'Caught exception: '. $e->getMessage(). "\n";
$status = FALSE;
echo json_encode(array('status' => $status, 'error'=>$msg));
exit;
}
$insert = $this->up->save($data1);
$data1 = array();
}
}
if($posReq=='YES' or $posReq=='Y'){
$data1 = array(
'drRef' => $abbr . '2017' . sprintf("%07d", ($drCount + 1)),
'accID' => $accID,
'userID' => $this->session->userdata['id'],
// 'postRef' => trim(strtoupper($row['K'])),
// 'postBy' => $this->session->userdata['name'],
// 'postDate' => $date,
'totQty' => $totalQty,
'status' => 'PENDING FOR POSTING',
'isPosted' => 'No',
'isApproved' => 'Pending',
'approverID' => $this->session->userdata('head'),
'postReq' => $posReq,
'reqDate' => $reqD,
'deliveryAdd' => $dAdd,
'reason' => urldecode($rmi),
'spInst' => urldecode($spInst),
);
}else{
$data1 = array(
'drRef' => $abbr . '2017' . sprintf("%07d", ($drCount + 1)),
'accID' => $accID,
'userID' => $this->session->userdata['id'],
//'postRef' => $this->session->userdata('username'),
'postBy' => $this->session->userdata['name'],
'postDate' => $date,
'totQty' => $totalQty,
'status' => 'POSTED',
'isPosted' => 'Yes',
'isApproved' => 'Pending',
'approverID' => $this->session->userdata('head'),
'postReq' => $posReq,
'tat' => $TATnAGING ,
'reqDate' => $reqD,
'deliveryAdd' => $dAdd,
'reason' => urldecode($rmi),
'spInst' => urldecode($spInst),
);
}
$insert = $this->up->saveOrder($data1);
$drRef = $abbr . '2017' . sprintf("%07d", ($drCount + 1));
$id = $this->up->get_Data('head','tblUser','userID',$this->session->userdata('id'));
$recepient = $this->up->get_Data('email','tblUser','userID',$id);
$name = $this->up->get_Data('name','tblUser','userID',$id);
$config = Array(
'protocol' => 'smtp',
'smtp_host' => '182.50.151.61',
'smtp_port' => 587,
'smtp_user' => 'user',
'smtp_pass' => 'pass',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
// Set to, from, message, etc.
$this->email->from('eDR#domain.com', 'e - Delivery Receipt');
$this->email->to($recepient);
//$this->email->subject('eDR Notifications');
$this->email->subject('eDR Notification < DR Request >');
$message = 'Message';
$this->email->message($message);
$result = $this->email->send();
// if (!$result) {
// $this->email->print_debugger();
// }
$receiver = $this->up->get_Data('phoneNum','tblUser','userID',$this->session->userdata('head'));
$sms = array(
'receiver' => $receiver,
'msg' => 'message',
'status' => 'Send',
);
$this->up->sendSMS($sms);
}
Thanks.
I solved this problem via CRON job or calling cmd command on my PHP script so that it will execute the query on server itself really fast. :)

facing problems uploading image in a form using codeigniter

Am trying to include an image in a form, the image is added correctly but however two rows are added in the database table.
Structure of table with data added is below:
My controller
public function addRecordToTable(){
$this->load->library('form_validation');
$this->form_validation->set_rules('client_id' , 'client_id', 'required');
$this->form_validation->set_rules('l_address' , 'location address', 'required|min_length[3]|max_length[50]');
if ($this->form_validation->run() == false) {
$this->load->model('Clientaccount_model');
$data['bids']=$this->Clientaccount_model->bids();
$data['loads']=$this->Truckeraccount_model->loads();
$this->load->view('footer');
} else {
$array = array(
'client_id' => $this->input->post('client_id'),
'l_address' => $this->input->post('l_address'),
);
$record_id = $this->consignmentupload_model->addData('consignment', $array);
$this->uploadFiles($record_id);
}
}
public function uploadFiles($record_id){
$config = array(
'upload_path' => FCPATH . "/uploads/",
'allowed_types' => 'jpg|png|jpeg',
'overwrite' => TRUE,
);
$this->load->library('upload', $config);
$files = $_FILES['uploads'];
foreach ($files['name'] as $key => $filename) {
$_FILES['uploads[]']['name'] = $files['name'][$key];
$_FILES['uploads[]']['type'] = $files['type'][$key];
$_FILES['uploads[]']['tmp_name'] = $files['tmp_name'][$key];
$_FILES['uploads[]']['error'] = $files['error'][$key];
$_FILES['uploads[]']['size'] = $files['size'][$key];
$config['file_name'] = $filename;
$this->upload->initialize($config);
if (isset($_FILES['uploads[]']['name']) && !empty($_FILES['uploads[]']['name'])) {
if ( ! $this->upload->do_upload('uploads[]')) {
$error = array('error' => $this->upload->display_errors());
} else {
$uploads[] = $this->upload->data();
$array = array(
'record_id' => $record_id,
'filename' => $_FILES['uploads[]']['name'],
'size' => $_FILES['uploads[]']['size']
);
$this->consignmentupload_model->addData('consignment', $array);
}
}
}
redirect(site_url('clientaccount_ctrl'));
}
My Model
public function addData($table, $array)
{
$this->db->insert($table, $array);
return $this->db->insert_id();
}
after your first insert that is,
$record_id = $this->consignmentupload_model->addData('consignment', $array);
you have to update the same row with your image,
so in uploadFiles instead of, $this->consignmentupload_model->addData('consignment', $array);
make a new model function updateData() where you update the same row which was inserted earlier, pass the luggage_id to updateData()

Codeigniter:Update Image and Display Current image if I don't want to update

I'm stuck on a problem with updating image. I've created image upload which works fine but I also want it to be updated. When I add a need image it updates correctly but I if don't want to change the image and leave it as it is, then my current image can't be retrieve. Please help me
Here is my code:
Controller:
function edit_product($product_id)
{
$data = array('recipe_info' => $this->products_model->getRcipe($this->uri->segment(3)),
'product_id' => $this->uri->segment(3)
);
/*var_dump($data); die();*/
$this->load->view('edit_product', $data);
}
public function save_edit_recipe()
{
$thumnail="";
$stats = "";
$msg = "";
$filename = 'r_image';
$data = array(
'recipe_id' => $this->uri->segment(3),
'r_name' => $this->input->post('r_name'),
'r_image' => '',
'r_description' => $this->input->post('r_description'),
'time_id' => $this->input->post('cooking_time'),
'r_cal' => $this->input->post('calories'),
'r_serve' => $this->input->post('serving_size'),
'r_procedure' => $this->input->post('recipe_procedure')
);
$recipe_id = $this->products_model->update_product($this->uri->segment(3), $data);
foreach($this->input->post('ingredients') as $key => $value)
{
$menuData[] = array('recipe_id' => $this->input->post('recipe_id'),
'ingredient_id' => $value,
'category_id' => $this->input->post('recipe_category')
);
}
$this->products_model->saveMenu($menuData);
$config['upload_path'] = 'img/recipes/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024 * 8 ;
$config['encrypt_name'] = true;
$this->load->library('upload',$config);
if (!$this->upload->do_upload($filename))
{
$stats = 'error';
$msg = $this->upload->display_errors('', '');
}else{
$uploadData = $this->upload->data('file_name');
if($uploadData['file_name'])
{
$thumnail = 'img/recipes/'.$uploadData['file_name'];
}else{
$thumnail = 'No thumnbail uploaded!';
}
$updateThumbData = array('recipe_id' => $this->input->post('recipe_id'),
'r_image' => $thumnail
);
$this->products_model->updataRecipeThumnail($updateThumbData);
}
redirect('dashboard');
}
Model:
function getRcipe($recipe_id)
{
$this->db->where('recipe_id', $recipe_id);
$query = $this->db->get('recipe');
return $query->result();
}
public function update_product($product_id, $data)
{
$this->db->where('recipe_id', $product_id);
$this->db->update('recipe', $data);
}
public function updataRecipeThumnail($data)
{
$this->db->where('recipe_id', $data['recipe_id']);
$this->db->update('recipe', $data);
}
i have updated in your script...please used this function... then your problem may be shortout..
public function save_edit_recipe()
{
$thumnail="";
$stats = "";
$msg = "";
$filename = 'r_image';
$data = array(
'recipe_id' => $this->uri->segment(3),
'r_name' => $this->input->post('r_name'),
'r_description' => $this->input->post('r_description'),
'time_id' => $this->input->post('cooking_time'),
'r_cal' => $this->input->post('calories'),
'r_serve' => $this->input->post('serving_size'),
'r_procedure' => $this->input->post('recipe_procedure')
);
$recipe_id = $this->products_model->update_product($this->uri->segment(3), $data);
foreach($this->input->post('ingredients') as $key => $value)
{
$menuData[] = array('recipe_id' => $this->input->post('recipe_id'),
'ingredient_id' => $value,
'category_id' => $this->input->post('recipe_category')
);
}
$this->products_model->saveMenu($menuData);
$config['upload_path'] = 'img/recipes/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024 * 8 ;
$config['encrypt_name'] = true;
$this->load->library('upload',$config);
if (!$this->upload->do_upload($filename))
{
$stats = 'error';
$msg = $this->upload->display_errors('', '');
}else{
$uploadData = $this->upload->data('file_name');
if($uploadData['file_name'])
{
$thumnail = 'img/recipes/'.$uploadData['file_name'];
$updateThumbData = array(
'recipe_id' => $this->input->post('recipe_id'),
'r_image' => $thumnail
);
$this->products_model->updataRecipeThumnail($updateThumbData);
}else{
$thumnail = 'No thumnbail uploaded!';
}
}
redirect('dashboard');
}

CodeIgniter Insert query makes extra entry with 0 values

When I trying to insert some values, MySQL shows double entry. The first entry is correct, but second entry contains only zeros. Kindly help me..
My Controller codes are here:
$config['upload_path'] = './img/trainees/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$imgname ='';
if($this->upload->do_upload())
{
$data = $this->upload->data();
$imgname = $data['raw_name'].$data['file_ext'];
}
Above code is used to upload images in controller..
Remaining codes:
$this->load->model('newmodel');
$this->newmodel->NAME = $this->input->post("name");
$this->newmodel->AGE = $this->input->post("age");
$this->newmodel->PLACE = $this->input->post("place");
$this->newmodel->ADDRESS = $this->input->post("address");
$this->newmodel->PHONE = $this->input->post("phone");
$this->newmodel->MAIL = $this->input->post("email");
$this->newmodel->QUALI = $this->input->post("qualification");
$this->newmodel->OCCUP = $this->input->post("occupation");
$this->newmodel->MILMAID = $this->input->post("memberID");
$this->newmodel->COURSE = $this->input->post("course");
$this->newmodel->CENTRE = $this->input->post("centre");
$this->newmodel->REGDATE = date('d/m/Y');
$this->newmodel->IMGNAME= $imgname;
$mydata = $this->newmodel->insertuserdata();
$data = array(
'name' => $this->newmodel->NAME,
'age' => $this->newmodel->AGE,
'place' => $this->newmodel->PLACE,
'address' => $this->newmodel->ADDRESS,
'phone' => $this->newmodel->PHONE,
'email' => $this->newmodel->MAIL,
'quali' => $this->newmodel->QUALI,
'occup' => $this->newmodel->OCCUP,
'milmaid' => $this->newmodel->MILMAID,
'course' => $this->newmodel->COURSE,
'centre' => $this->newmodel->CENTRE,
'regdate' => $this->newmodel->REGDATE,
'imgname' => $this->newmodel->IMGNAME,
);
$this->load->helper('url');
$this->load->view('users/admitcard',$data);
My Model codes are seem like this:
var $NAME = '';
var $AGE = 0;
var $PLACE = '';
var $ADDRESS = '';
var $PHONE = 0;
var $MAIL = '';
var $QUALI = '';
var $OCCUP = '';
var $MILMAID = '';
var $COURSE = '';
var $CENTRE = '';
var $REGDATE = '';
var $IMGNAME ='';
public function insertuserdata(){
$this->load->database();
$datas = array(
'Tname' => $this->NAME,
'Tage' => $this->AGE,
'Tplace' => $this->PLACE,
'TAddres' => $this->ADDRESS,
'Tph' => $this->PHONE,
'Tmail' => $this->MAIL,
'Tqualification' => $this->QUALI,
'Toccupation' => $this->OCCUP,
'TmilmaMemb' => $this->MILMAID,
'Tcourse' => $this->COURSE,
'Tcentre' => $this->CENTRE,
'regDate' => $this->REGDATE
);
$this->db->insert('tbl_trainers', $datas);
Try this code on your model
public function insertuserdata(){
$this->load->database();
$datas = array(
'Tname' => $this->input->post("name"),
'Tage' => $this->input->post("age"),
'Tplace' => $this->input->post("place"),
'TAddres' => $this->input->post("address"),
'Tph' => $this->input->post("phone"),
'Tmail' => $this->input->post("email"),
'Tqualification' => $this->input->post("qualification"),
'Toccupation' => $this->input->post("occupation"),
'TmilmaMemb' => $this->input->post("memberID"),
'Tcourse' => $this->input->post("course"),
'Tcentre' => $this->input->post("centre"),
'regDate' => date('d/m/Y')
);
$this->db->insert('tbl_trainers', $datas);
}
And simply call this model on your controller. Now your controller look like this-
public function yourMethod(){
$this->load->model('newmodel');
$this->newmodel->insertuserdata();
# And your rest code here...
}

Codeigniter 2.1 - fileupload You did not select a file to upload

Model function:
public function file_upload($folder, $allowed_type, $max_size = 0, $max_width = 0, $max_height = 0)
{
$folder = $this->path . $folder;
$files = array();
$count = 0;
foreach ($_FILES as $key => $value) :
$file_name = is_array($value['name']) ? $value['name'][$count] : $value['name'];
$file_name = $this->global_functions->char_replace($file_name, '_');
$count++;
$config = array(
'allowed_types' => $allowed_type,
'upload_path' => $folder,
'file_name' => $file_name,
'max_size' => $max_size,
'max_width' => $max_width,
'max_height' => $max_height,
'remove_spaces' => TRUE
);
$this->load->library('image_lib');
$this->image_lib->clear();
$this->load->library('upload');
$this->upload->initialize($config);
if (!$this->upload->do_upload($key)) :
$error = array('error' => $this->upload->display_errors());
var_dump($error);
return FALSE;
else :
$file = $this->upload->data();
$files[] = $file['file_name'];
endif;
endforeach;
if(empty($files)):
return FALSE;
else:
return implode(',', $files);
endif;
}
This function is working partially. Files are being uploaded to the selected folder, but I am getting this error: You did not select a file to upload. and getting FALSE as a result? What seems to be a problem? (form is using form_open_multipart)
Please make sure that you have this name of your file tag field:
$key='userfile' //which is name of input type field name
Are any of your file inputs empty when this happens? $_FILES will contain an array of "empty" data if there is no file specified for the input. For example, my file input name is one, and I submitted it without specifying a file:
Array
(
[one] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
You should check if the file data is empty before processing the upload. PHP's is_uploaded_file() is useful for this.

Categories