dqh1984 2015-09-12 09:28
浏览 35
已采纳

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

I am trying to insert records using csv. I want to upload csv to my Application and want to import it in database. Now I have a users table so i want to create users by importing user's csv file. I Know a little about file uploads but nil about importing it into database please help .

See I have done the file upload using : Controller:

       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);

      }

The Another 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);

}

And The view:

    <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>

Now How do i import a uploaded file into my database ??

  • 写回答

2条回答 默认 最新

  • dongliao1860 2015-09-12 09:36
    关注

    From a 2 second Google and the PHP docs:

    $csv = array_map('str_getcsv', file('data.csv'));
    

    Then you can loop through the Array and UPDATE your DB

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog