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 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多