tutorial restful react poo example api codeigniter rest codeigniter-2

api - restful - Codeigniter REST CSV import a mysql



php rest api (1)

Quiero publicar un archivo csv en mi controlador usando API. Estoy usando la biblioteca REST de Codeigniter por phil sturgeon. Cómo implementar en el lado del cliente la importación de CSV a mi servidor REST. Solo quiero preguntar porque no puedo encontrar ninguna documentación al respecto.


Aquí hay una manera fácil de hacer esto. No sé qué hace la gente, pero yo uso esto

Esta es mi biblioteca de lectores csv, guárdala en la carpeta de bibliotecas como 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; } } ?>

Y el método del controlador

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); }

Y esto es vista

<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>

Nota: Esto solo leerá un archivo que existe en el servidor. Si es necesario cargar el archivo, utilice la Clase de carga de archivos para cargar el archivo y guárdelo en una ubicación en su servidor, luego proporcione la ruta del archivo ubicado en el método parse_file . Y todo funcionará bien.