我正在嘗試將數(shù)據(jù)從 csv 文件上傳到我的 sql server 數(shù)據(jù)庫(kù)。所以我創(chuàng)建了這個(gè) csv 閱讀器庫(kù):<?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; }}?>然后我試圖從文件中開始對(duì)我的數(shù)據(jù)檢索部分進(jìn)行編碼,以便我可以將它們發(fā)送到模型以便將它們插入到數(shù)據(jù)庫(kù)中。但我收到此錯(cuò)誤:無(wú)法打開流:沒(méi)有這樣的文件或目錄這是指向fopen指令。你能幫我解決這個(gè)問(wèn)題嗎?
1 回答

函數(shù)式編程
TA貢獻(xiàn)1807條經(jīng)驗(yàn) 獲得超9個(gè)贊
因?yàn)槟鷽](méi)有指向完整路徑(包括文件夾)
嘗試
<?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 關(guān)注
- 151 瀏覽
添加回答
舉報(bào)
0/150
提交
取消