我的 php 代码只是将我的 csv 文件的第一行导入数据库
Posted
技术标签:
【中文标题】我的 php 代码只是将我的 csv 文件的第一行导入数据库【英文标题】:my php code is only importing the first line of my csv file to the database 【发布时间】:2017-03-08 15:55:51 【问题描述】:所以,我有一个要导入数据库的 csv 文件,但它只导入第一行。 我不知道为什么它不导入我的 csv 文件中的所有数据。 可能是换行的原因?
我的 CSV 文件示例:
“蒂亚戈”,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 "克劳迪奥" ,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,21,sip/21,20,w,2016-03-01 14:39:55,2016-03-01 14:40:12,2016-03-01 14:40: 25,30 “安德烈”,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();
【问题讨论】:
$linecount=count(file($path.''.$filename.'.csv'));
将始终等于 1
。它只是在计算数组中的条目数,即一个...
【参考方案1】:
您必须遍历每一行才能读取整个文件,否则您只会读取文件中的第一行
`
$file = fopen($path.''.$filename.'.csv', "r");
$csv = array();
while (($csv = fgetcsv($file, 0, ",", ' ')) !== FALSE)
$n[] = $csv;
fclose($file);
`
【讨论】:
感谢您的帮助我解决了它,如果您的代码现在感谢它提供了所有数据 现在只需要知道导入数据库 你应该在循环内写“INSERT”查询 如果有人想要我可以发布最终解决方案以上是关于我的 php 代码只是将我的 csv 文件的第一行导入数据库的主要内容,如果未能解决你的问题,请参考以下文章