我的 php 代码只将我的 csv 文件的第一行导入到数据库
my php code is only importing the first line of my csv file to the database
所以,我有一个要导入数据库的 csv 文件,但它只导入了第一行。
我不知道为什么它不导入我的 csv 文件中的所有数据。
可能是因为换行符?
我的 CSV 文件示例:
"Tiago" <20>,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
"Claudio" <10>,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
"Hortencio" <11>,11,21,sip/21,20,w,2016-03-01 14:39:55,2016-03-01 14:40:12,2016-03-01 14:40:25,30
"Andre" <19>,19,22,22000@default,s,2016-03-01 14:43:22,2016-03-01 14:43:42,2016-03-01 14:43:42,20
我的模型:
<?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();
}
}
您必须遍历每一行才能读取整个文件,否则您只会读取文件中的第一行
`
$file = fopen($path.''.$filename.'.csv', "r");
$csv = array();
while (($csv = fgetcsv($file, 0, ",", ' ')) !== FALSE)
{
$n[] = $csv;
}
fclose($file);
`
所以,我有一个要导入数据库的 csv 文件,但它只导入了第一行。 我不知道为什么它不导入我的 csv 文件中的所有数据。 可能是因为换行符?
我的 CSV 文件示例:
"Tiago" <20>,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 "Claudio" <10>,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 "Hortencio" <11>,11,21,sip/21,20,w,2016-03-01 14:39:55,2016-03-01 14:40:12,2016-03-01 14:40:25,30 "Andre" <19>,19,22,22000@default,s,2016-03-01 14:43:22,2016-03-01 14:43:42,2016-03-01 14:43:42,20
我的模型:
<?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();
}
}
您必须遍历每一行才能读取整个文件,否则您只会读取文件中的第一行
`
$file = fopen($path.''.$filename.'.csv', "r");
$csv = array();
while (($csv = fgetcsv($file, 0, ",", ' ')) !== FALSE)
{
$n[] = $csv;
}
fclose($file);
`