Manipulate .asc file with PHP and MySQL

0

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:

  • Programming the "select file" button
  • select the file
  • Upload it and upload it to my BD
  • 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);
                }     
            }      
    }
    
        
    asked by Rickzize 26.12.2016 в 09:18
    source

    2 answers

    0

    If you want to insert each value separated by "pipe" in a field of a table in the Database, you could try the following:

    $contenido = file_get_contents(path/to/file/uploaded);
    
    if ( $contenido != "" )
    {
         $lineas = explode("\n", $contenido);
    
         if ( count($linias) > 0 )
         {
             foreach ( $lineas AS $linea )
             {
                 if ( $linea != "" )
                 {  
                     $campos = explode("|", $linea);
                     if ( count($campos) > 0 )
                     {
                         foreach ( $campos AS $campo )
                         {  
                            $data = [
                               'campo1' => $campo[.....],
                                ......,
                                'campoN' => $campo[.....],  
                            ];
    
                            // Dando por hecho que tienes un modelo y un 
                            // método en él para ejecutar los inserts en la DB
                            $this->model->insertar($row);
                          }
                    }
                }
            }
        }
     }
     ......... validación de los inserts, etc
    

    I hope you find it useful.

    Greetings

        
    answered by 28.02.2017 в 23:53
    0

    Apparently you are using CodeIgniter, so I checked the class code Csvimport in GitHub . The way to indicate the delimiter is to use the $delimiter parameter in the get_array method call:

    $this->csvimport->get_array($file_path, FALSE, FALSE, FALSE, '|')
    
        
    answered by 01.03.2017 в 01:10