我的PHP代码只导入我的csv文件的第一行到数据库

时间:2016-10-26 10:20:10

标签: php mysql codeigniter csv fgetcsv

所以,我有一个csv文件,我想导入到数据库,但它只导入第一行。 我不知道为什么它不导入我的csv文件中的所有数据。 也许是因为换行?

我的CSV文件示例:

&#34;蒂亚戈&#34; &lt; 20&gt;,20,11,sip / 11,20,w,2016-03-01 14:33:06,2016-03-01 14:33:09,2016-03-01 14:33:51, 45 &#34;克劳迪奥&#34; &lt; 10&gt;,10,11,sip / 11,20,w,2016-03-01 14:35:05,2016-03-01 14:35:07,2016-03-01 14:35:48, 43 &#34; Hortencio&#34; &lt; 11&gt;,11,21,sip / 21,20,w,2016-03-01 14:39:55,2016-03-01 14:40:12,2016-03-01 14:40:25,三十 &#34;安德烈&#34; &lt; 19&gt;,19,22,22000 @ default,s,2016-03-01 14:43:22,2016-03-01 14:43:42,2016-03-01 14:43:42,20 < / p>

我的模特:

<?php

class Csv_model extends CI_Model {

var $gallery_path;
var $gallery_path_url;
function __construct() {
    parent::__construct();


    $this->gallery_path = realpath(APPPATH . '../csv');
    $this->gallery_path_url = base_url() . 'csv/';
}

function do_upload($data) {
        $filename = $data['fileinfo'][0];
        $path = $data['fileinfo'][1];
        ini_set("auto_detect_line_endings", true);
        if (!$this->upload->do_upload('userfile')) {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('csv_form', $error);
        } else {
            $data = array('upload_data' => $this->upload->data());
            $this->load->view('csv_success', $data);
            $linecount=count(file($path.''.$filename.'.csv'));

            $file = fopen($path.''.$filename.'.csv', "r");

            $csv = fgetcsv($file, 0, ",", ' ');
            fclose($file);


            $n = 0;
            echo $l=$linecount;
            function validateDate($date, $format = 'Y-m-d H:i:s'){
                 $d = DateTime::createFromFormat($format, $date);
                 return $d && $d->format($format) == $date;
            }
            for($i = 0; $i <= $l && $n < $l; $i++){

                $insert_csv = array();

                $insert_csv['clid'] = $csv[$n];
                $n++;
                $insert_csv['src'] = $csv[$n];
                $n++;
                $insert_csv['dst'] = $csv[$n];
                $n++;
                $insert_csv['dstchannel'] = $csv[$n];
                $n++;
                if(is_numeric($csv[$n])){
                    echo 'yes';
                $insert_csv['numero'] = $csv[$n];
                $n++;
                }else{
                echo"ok";
                $insert_csv['numero'] = " ";}
                if(ctype_alpha($csv[$n])){
                $insert_csv['w'] = $csv[$n];
                $n++;
                }else{
                $insert_csv['w'] = " ";
                echo"ok";}
                $insert_csv['start'] = $csv[$n];
                $n++;
                $insert_csv['answer'] = $csv[$n];
                $n++;
                if(validateDate($csv[$n])){
                $insert_csv['end'] = $csv[$n];
                $n++;
                }else{
                $insert_csv['end'] = " ";}
                if($csv[$n] == 0){
                $insert_csv['duration'] = " ";
                $n++;}else{
                $insert_csv['duration'] = $csv[$n];
                $n++;}


                $data1 = array(
                'clid' => $insert_csv['clid'],
                'src' => $insert_csv['src'],
                'dst' => $insert_csv['dst'],
                'dstchannel' => $insert_csv['dstchannel'],
                'numero' => $insert_csv['numero'],
                'w' => $insert_csv['w'],    
                'start' => $insert_csv['start'],
                'answer' => $insert_csv['answer'],
                'end' => $insert_csv['end'],
                'duration' => $insert_csv['duration']);

                $this->db->insert('chamadas', $data1);

            }
            echo 'Success!!!';
            $success['success'] = "success";
            return $success;

        }
} 

function get_all()
    {
        return $this->db->get('chamadas')->result_array();
    }

}

1 个答案:

答案 0 :(得分:3)

您必须遍历每一行才能读取整个文件,否则您只需阅读文件中的第一行

`

$file = fopen($path.''.$filename.'.csv', "r");
$csv = array();  
while (($csv = fgetcsv($file, 0, ",", ' ')) !== FALSE) 
{
   $n[] = $csv;
}
fclose($file);

`