我正在尝试将数据从 csv 文件上传到我的 sql server 数据库。所以我创建了这个 csv 阅读器库:<?phpif (!defined('BASEPATH')) exit('No direct script access allowed');class CSVReader{ var $fields; /** columns names retrieved after parsing */ var $separator = ';'; /** separator used to explode each line */ var $enclosure = '"'; /** enclosure used to decorate each field */ var $max_row_size = 4096; /** maximum row size to be used for decoding */ function parse_file($p_Filepath) { $file = fopen($p_Filepath, 'r'); $this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure); $keys_values = explode(',', $this->fields[0]); $content = array(); $keys = $this->escape_string($keys_values); $i = 1; while (($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false) { if ($row != null) { // skip empty lines $values = explode(',', $row[0]); if (count($keys) == count($values)) { $arr = array(); $new_values = array(); $new_values = $this->escape_string($values); for ($j = 0; $j < count($keys); $j++) { if ($keys[$j] != "") { $arr[$keys[$j]] = $new_values[$j]; } } $content[$i] = $arr; $i++; } } } fclose($file); return $content; } function escape_string($data) { $result = array(); foreach ($data as $row) { $result[] = str_replace('"', '', $row); } return $result; }}?>然后我试图从文件中开始对我的数据检索部分进行编码,以便我可以将它们发送到模型以便将它们插入到数据库中。但我收到此错误:无法打开流:没有这样的文件或目录这是指向fopen指令。你能帮我解决这个问题吗?
1 回答
函数式编程
TA贡献1807条经验 获得超9个赞
因为您没有指向完整路径(包括文件夹)
尝试
<?php
$data = $this->upload->data();
//$csvFile=FCPATH."upload/".$fileName; //php way (uploaded filepath and filename )
$csvFile=$data['full_path']; //codeigniter way
$result = $this->csvreader->parse_file($csvFile);
?>
- 1 回答
- 0 关注
- 132 浏览
添加回答
举报
0/150
提交
取消