I want to upload a file .asc
and insert it into my MySQL database.
The content of the file is similar to this:
3884 | 5000007 | 160 | 1 | IN | 160 || HME980330SL3 | MESJ540212HDFRNR02 | 14.71800 | 0 | 0 | 0 | 0 | 0 | 8827.700 | 7 | 1 | 1 | 9 | spartan K | CARRT. PANAMERICANA K.M. 5.5 | 2-A - L-C | SN | 36100 | SILAO | GT | MEX | 1 | 2015-01-06 16: 43: 49 | 2015-01-06 16: 54: 50 |
The idea is this:
Any ideas on how to do it? this is my driver csv.php the file is separated by pipes try to manipulate it as .csv but I do not know how to change the "," by pipes.
<?php
class Csv extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('csv_model');
$this->load->library('csvreader');
$this->load->library('csvimport');
}
function index() {
$data['addressbook'] = $this->csv_model->get_addressbook();
$this->load->view('csvindex', $data);
}
function importcsv() {
$data['addressbook'] = $this->csv_model->get_addressbook();
$data['error'] = ''; //initialize image upload error array to empty
$config['upload_path'] = './static/uploads';
$config['allowed_types'] = 'csv';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
// If upload failed, display error
if (!$this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
$this->load->view('csvindex', $data);
} else {
$file_data = $this->upload->data();
$file_path = './static/uploads/'.$file_data['file_name'];
if ($this->csvimport->get_array($file_path)) {
$csv_array = $this->csvimport->get_array($file_path);
foreach ($csv_array as $row) {
$insert_data = array(
'firstname'=>$row['firstname'],
'lastname'=>$row['lastname'],
'phone'=>$row['phone'],
'email'=>$row['email'],
);
$this->csv_model->insert_csv($insert_data);
}
$this->session->set_flashdata('success', 'Tu archivo a sido subido exitosamente');
redirect(base_url().'csv');
echo "<pre>"; print_r($insert_data);
} else
$data['error'] = "Error occured";
$this->load->view('csvindex', $data);
}
}
}