Codeigniter REST CSV导入到MySQL

我想使用API​​在控制器上发布csv文件。我使用phil sturgeon的Codeigniter REST Library。 如何在客户端实现CSV导入到我的REST服务器。我只是想问,因为我找不到任何有关它的文档。

这是一个简单的方法来做到这一点。 我不知道人们做什么,但我使用这个

这是我的csv阅读器库,将其保存在库文件夹中作为csvreader.php。

 <?php if (!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; } } ?> 

和控制器方法

 function readExcel() { $this->load->library('csvreader'); $result = $this->csvreader->parse_file('Test.csv');//path to csv file $data['csvData'] = $result; $this->load->view('view_csv', $data); } 

这是观点

 <table cellpadding="0" cellspacing="0" width="100%"> <tr> <td width = "10%">ID</td> <td width = "20%">NAME</td> <td width = "20%">SHORT DESCRIPTION</td> <td width = "30%">LONG DESCRIPTION</td> <td width = "10%">STATUS</td> <td width = "10%">PARENTID</td> </tr> <?php foreach($csvData as $field){?> <tr> <td><?php echo $field['id']?></td> <td><?php echo $field['name']?></td> <td><?php echo $field['shortdesc']?></td> <td><?php echo $field['longdesc']?></td> <td><?php echo $field['status']?></td> <td><?php echo $field['parentid']?></td> </tr> <?php }?> </table> 

注意:这只会读取服务器上存在的文件。 如果文件需要上传,使用file upload类上传文件并将其保存到服务器上的某个位置,然后在parse_file方法中给定位文件的path。 一切都会正常工作。