如何使用Codeigniter PHP在数据库中导入CSV?

Raj*_*jan 0 php csv import codeigniter file-upload

我试图使用csv插入记录.我想将csv上传到我的应用程序,并希望将其导入数据库.现在我有一个用户表,所以我想通过导入用户的csv文件来创建用户.我知道一些关于文件上传但是关于将其导入数据库的问题请帮忙.

请参阅我使用以下命令完成文件上传:控制器:

       public function upload_csv()
      {
            $this->load->helper('form');
            $this->load->helper('url');

            //Set the message for the first time
            $data = array('msg' => "Upload File");

            $data['upload_data'] = '';

            //load the view/upload.php with $data
            $this->load->view('admin/user/upload', $data);

      }
Run Code Online (Sandbox Code Playgroud)

另一个Controller Upload_file

public function upload_it() {
    //load the helper
    $this->load->helper('form');

    //Configure
    //set the path where the files uploaded will be copied. NOTE if using linux, set the folder to permission 777
    $config['upload_path'] = 'application/views/uploads/';

// set the filter image types
    $config['allowed_types'] = 'gif|csv';

    //load the upload library
    $this->load->library('upload', $config);

$this->upload->initialize($config);

$this->upload->set_allowed_types('*');

    $data['upload_data'] = '';

    //if not successful, set the error message
    if (!$this->upload->do_upload('userfile')) {
        $data = array('msg' => $this->upload->display_errors());

    } else { //else, set the success message
        $data = array('msg' => "Upload success!");

  $data['upload_data'] = $this->upload->data();

    }

    //load the view/upload.php
    $this->load->view('admin/user/upload', $data);

}
Run Code Online (Sandbox Code Playgroud)

和观点:

    <code>
    <?php if($upload_data != ''):?>
    <?php var_dump($upload_data);?>

    </code>
    <img scr="<?php echo $upload_data['full_path'];?>">
    <?php endif;?>

    <?php echo form_open_multipart('admin/upload_file/upload_it');?>

    <input type="file" name="userfile" size="20" />

    <br /><br />

    <input type="submit" value="upload" />

    </form>
Run Code Online (Sandbox Code Playgroud)

现在如何将上传的文件导入我的数据库?

ham*_*mus 6

来自2秒的Google和PHP文档:

$csv = array_map('str_getcsv', file('data.csv'));
Run Code Online (Sandbox Code Playgroud)

然后,您可以遍历数组和UPDATE数据库