如何在Codeigniter中使用jQuery ajax将csv文件导入数据库并移动到文件夹中?

时间:2019-01-09 05:13:55

标签: jquery codeigniter csv mysqli import

观看次数:

<script>
    $(document).ready(function(){
        $('#upload_csv').on("submit",function(e){  
                    e.preventDefault(); 
                    $.ajax({  
                         url:"<?php echo base_url(); ?>import",  
                         method:"POST",  
                         data:new FormData(this),  
                         contentType:false, 
                         cache:false, 
                         processData:false,  
                         success: function(data){  
                              if(data=='Error1')  
                              {  
                                   alert("Invalid File");  
                              }  
                              else if(data == "Error2")  
                              {  
                                   alert("Please Select File");  
                              }  
                              else  
                              {  
                                   $('#success').html(data);  
                              }  
                         }  
                    })  
               });  
    });
</script>
<form id="upload_csv" method="post" enctype="multipart/form-data">
    <input type="file" id="files" name="files">
    <input type="submit" class="btn btn-primary" id="submit" name="submit">
</form>

控制器:

public function import()
{
    if(!empty($_FILES["files"]["name"]))  
    {   
        $output = '';  
        $allowed_ext = array("csv");  
        $tmp = explode(".", $_FILES["files"]["name"]);  
        $extension = end($tmp);
        if(in_array($extension, $allowed_ext))  
        {  
            $file_data = fopen($_FILES["files"]["tmp_name"], 'r');  
            print_r(fgetcsv($file_data));

        }  
        else  
        {  
            echo 'Error1';  
        }  
    }  
    else  
    {  
        echo "Error2";  
    }  
}

在这段代码中,我想在数据库中导入csv file,还将CSV文件移动到文件夹中。现在,当我单击提交按钮时,我正在使用print_r(fgetcsv($file_data));来了解csv文件的输出然后打印

Array ( [0] => Id [1] => first_name [2] => last_name [3] => phone [4] => email )

但是我的csv文件中有200行。那么,我该怎么做?请帮助我。

谢谢

1 个答案:

答案 0 :(得分:0)

在您执行导入功能

  public function import()
            {
                    $config['upload_path']          = './uploads/';
                    $config['allowed_types']        = 'csv';

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

                    if ( ! $this->upload->do_upload('files'))
                    {
                            print_r($this->upload->display_errors());
                            EXIT;
                    }
                    else
                    {
                    $fData = $this->upload->data();        
    $data = array();
    $file = fopen($fData['file_name'], "r");
             while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
             {
                $data[] = array(
                'fieldname1' => $emapData[0];
                'fieldname2' => $emapData[1];
                );
    // array for active record 
             }
             fclose($file);
             $this->load->model('any_model');
             $this->any_model->batch_data($data);
             echo "CSV File has been successfully Imported.";

                    }
            }
    }

型号:

function batch_data($data = array())
{
 $this->db->insert_batch('table_name',$data);
}

您需要遵循的步骤...