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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵