如何使用codeigniter将CSV文件的数据上传到mysql

时间:2016-08-23 07:38:28

标签: codeigniter csv

我正在尝试使用codeigniter将一个csv文件上传到mysql中我尝试了很长时间但是一切都在静脉中。我在这里分享我的代码..每次点击上传按钮都不起作用。

form.php的

<div class="portlet-body form">
    <!-- BEGIN FORM-->

                 

    <?php if($this->session->flashdata('success')== TRUE) ?> 
        <div class="alert alert-success"><?php echo $this->session->flashdata('success'); ?></div>

    <form name="domestic_csv" action="<?= base_url() ?>controller/add_csv" class="form-horizontal" enctype="multipart/form-data" method="post">
        <div class="form-body">

            <div class="form-group">
            <li class="ui-field"><label for="csvfile">Upload data id file( *only .csv)</label></li> <br>
                <label class="col-md-3 control-label">Upload Data</label>
                <div class="col-md-4">
                    <div class="input-group">

                        <input type="file" name="userfile" class="form-control" placeholder="Upload Data in CSV format"> </div>
                </div>
            </div>              

        <div class="form-actions top">
            <div class="row">
                <div class="col-md-offset-3 col-md-9">
                    <button type="submit" class="btn green">Upload</button>
                    <button type="button" class="btn default">Cancel</button>
                </div>
            </div>
        </div>
        </div>
    </form>
    <!-- END FORM-->
</div>

Controller.php这样

public function add_csv()
{
    $data['error']='';
    $config['upload_path']='../upload/';
    $config['allowed_types']='csv';
    $config['max_size']='90000';

    $this->load->library('upload',$config);
    if(!$this->upload->do_upload()){
        $data['error']= $this->upload->display_errors();
        //$this->load->view('csv',$data);
        $this->load->view('form',$data);                
    }
    else{
        $file_data = $this->upload->data();
        $file_path = '../upload/'.$file_data['userfile'];

        if($this->csvimport->get_array($file_path)){
            $csv_array= $this->csv_import->get_array($file_path);

            foreach($csv_array as $row){
               $insert_data = array('id'=>$row['id'],
                                    'name'=>$row['name'],
                                    'phone'=>$row['phone'], );

                   $this->main_model->insert_csv($insert_data);
                }

                $this->session->set_flashdata('success','The data Inserted!');
                redirect('controller/form');
            }

            else{
                $data['error'] = 'Error occoured';
                $this->load->view('form',$data);
                }
        }
}

main_model.php

function insert_csv($data){
    $this->db->insert('notebook',$data);
    return 1; 
}

1 个答案:

答案 0 :(得分:0)

public function add_csv()
 {     
   $filename=$_FILES["file"]["tmp_name"];

           if($_FILES["file"]["size"] > 0)
           {
              $file = fopen($filename, "r");

                 while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
                   {
                      $insert_data = array('id'=>$row['id'],
                                'name'=>$row['name'],
                                'phone'=>$row['phone'], );

                         $this->main_model->insert_csv($insert_data);
                   } 
                 }
                 fclose($file);

           }
  }