2018-03-24 14:41
浏览 69

CodeIgniter - 将CSV上传到数据库问题

I have an application where I want a user to be able to upload a list of contacts via CSV and save them to the database. I've tried to setup brad stinsons CSV codeigniter library but been having trouble so far... When I click on upload, the CSV just disappears and goes nowhere, without any errors..

If anyone could inform me on whether I'm on the right track or just completely off I'd really appreciate it.

Here is my Csv_import.php Controller

public function load_data() {

    $result = $this->csv_import_model->select();
    $output = 'contacttable';
    $count = 0;
    if($result->num_rows() > 0)

        foreach ($result->result() as $row)
            $count = $count + 1;
            $output .= '


    else {
        $output .='<p>Data not available</p>';

    $output .= '</table></div>';
    echo $output;


public function import()
    $file_data = $this->csvimport->get_array($_FILES["csv_file"] 

    foreach($file_data as $row)
        $data[] = array (

        'fullname' => $row["fullname"],
        'email' => $row["email"],
        'country' =>$row["country"],
        'gender' =>$row["gender"]


my Csv_Import_model.php model

class Csv_import_model extends CI_model
    function select()
        $this->db->order_by('idcontacts', 'DESC');
        $query = $this->db->get('contacts');
        return $query; 

    function insert($data)
        $this->db->insert_batch('contacts', $data);

My View on dashboard

<form method="post" id="import_csv" enctype="multipart/form-data">
    <div class="form-group">
        <label>Select CSV File</label>
        <input type="file" name="csv_file" id="csv_file" required accept=".csv" />

    <button type="submit" name="import_csv" class="btn btn-info" 
    id="import_csv_btn">Import CSV</button>
<div id="imported_csv_data"></div>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzhi3105 2018-03-26 20:28

    I was able to get this working by following the git repository in this link

    打赏 评论
  • doucan8521 2018-03-25 12:58

    As stated in the Github page of bradstinson/csv-import:


    CSV Import is a Codeigniter spark which makes it easy to import a CSV file into an associative array.

    NOTE: I've not used this code in about 3 years, and will no longer offer any type of support. However, feel free to use this code at your own risk.

    As such, I strongly advised you against using a deprecated library for your application.

    Note that you don't have to look for a CSV library dedicated to CodeIgniter. Any PHP library can be loaded inside CI. This is preferably done through Composer autoloading.

    With a quick search, I could recommend:

    1. Did you know that PHP has its very own function (fgetcsv) to parse CSV file? It has its shortcomings (not respecting quoted string, or multilined CSV), but it may work in your case.

    2. parsecsv/parsecsv-for-php which implements the RFC 4180 (CSV standard) and looks actively maintained. It can be loaded with Composer.

    Here is an example using fgetcsv:

    if (($handle = fopen("myfile.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            // Process with $data

    Here is an example using parsecsv:

    $csv = new ParseCsv\Csv();
    $csv->delimiter = ",";
    foreach($csv->data as $row) {
        // Process using $row
    打赏 评论

相关推荐 更多相似问题