csv file upload in codeigniter with check header
Form
<form method="post" enctype="multipart/form-data" action="<?php echo base_url()."admin/amazon_fees_detail/uploadcsvamazonfeesdetail"; ?>"> <div class="form-group"> <label class = "col-sm-3 control-label" style="text-align: end;padding-top: 5px;">CSV File Upload</label> <div class="col-sm-2" style="text-align: end;padding-top: 5px;"> <input type="file" name="csv" id="csv" required accept=".csv"> </div> </div> <div class="form-group"> <div class="col-sm-1"> <button type="submit" class="btn btn-primary" id="Submit">Submit</button><br><br> </div> </div> </form>
Controller
public function uploadcsvamazonfeesdetail(){ if (!empty($_FILES["csv"]['name'])) { $fileName = $_FILES["csv"]["tmp_name"]; $count=0; $fp = fopen($fileName,'r') or die("can't open file"); $wrongUser=0; while($csv_line = fgetcsv($fp,1024)) { $count++; if($count == 1) { if($csv_line[0]=='Date' && $csv_line[1]=='Seller ID' && $csv_line[2]=='Invoice No.' && $csv_line[3]=='Amount' && $csv_line[4]=='Shipping Charges Amazon/Ad Fee' && $csv_line[5]=='PG Fees, Listing & Other Charges' && $csv_line[6]=='Storage Fee' && $csv_line[7]=='Fixed Closing Fee' && $csv_line[8]=='GST'){ continue; } else{ $this->session->set_flashdata('message', 'Upload same format which provided in sample csv'); redirect('admin/amazon_fees_detail', 'refresh'); } }//keep this if condition if you want to remove the first row for($cnt = 0; $cnt < count($csv_line); $cnt++) { // Get Usesr ID $get_mws_settings = $this->mws_settings_model->getmwssettingsbymws_sellerid($csv_line[1]); $user_id = $get_mws_settings['user_id']; if(!empty($user_id)) { $insert_csv = array(); $insert_csv['date'] = (empty($csv_line[0]) ? 'null' : $csv_line[0]); $insert_csv['user_id'] = (empty($csv_line[1]) ? 'null' : $user_id); $insert_csv['invoice_no'] = (empty($csv_line[2]) ? '' : $csv_line[2]); } else{ $wrongUser++; $wrongUserArray[] = $csv_line[1]; } } // get mws settings details by id //$get_mws_settings_details = $this->mws_settings_model->getmwssettingsbyid($insert_csv['user_id']); if(!empty($insert_csv)) { // data array $data = array( 'date' => date('Y-m-d', strtotime($insert_csv['date'])), 'user_id' => $insert_csv['user_id'], 'invoice_no' => $insert_csv['invoice_no'], ); // check seller settlement if(!empty($data['invoice_no'])){ $get_where = array( 'date =' => $data['date'], 'user_id =' => $data['user_id'], 'invoice_no =' => $data['invoice_no'] ); $seller_settlement = $this->seller_settlement_model->get_seller_settlement_details($get_where); if (empty($seller_settlement)) { // insert amazon_fee_calculation $this->seller_settlement_model->adddata($data, 'amazon_fee_calculation'); } else { // update amazon_fee_calculation $update_where = array( 'id' => $seller_settlement['id'], 'user_id' => $seller_settlement['user_id'], 'date' => $seller_settlement['date'], 'invoice_no' => $seller_settlement['invoice_no'] ); $this->seller_settlement_model->update_seller_settlement_details($data, $update_where); } } } } fclose($fp) or die("can't close file"); } if($wrongUser == 0){ $this->session->set_flashdata('message', 'File uploaded successfully'); } else{ $totalUsers = array_unique($wrongUserArray); if(!empty($totalUsers)){ $seller = implode(", ",$totalUsers); $this->session->set_flashdata('message', '('.$seller.') Seller IDs are wrong and not uploaded their data'); } else { $this->session->set_flashdata('message', 'File uploaded successfully'); } } redirect('admin/amazon_fees_detail', 'refresh');}
Model
public function adddata($data,$table){ $this->db->insert($table,$data); $insert_id = $this->db->insert_id(); return $insert_id;}
function update_seller_settlement_details($data, $update_where){ $this->db->where($update_where); return $this->db->update('amazon_fee_calculation', $data);}