12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- class Import {
-
- static function csv_reader($path)
- {
- return new CsvReader($path);
- }
-
- function csv_to_array($filename) {
- $result = array();
-
- $handle = fopen($filename, 'r');
- if ($handle === false) {
- return $result;
- }
- $buffer = array();
- $i = 0;
- while (!feof($handle) && $i < 200) {
-
- $buffer[] = fgets($handle);
- $i++;
- }
- fclose($handle);
- $buffer = implode("\n", $buffer);
- $from_encoding = api_detect_encoding($buffer);
- unset($buffer);
-
- $handle = fopen($filename, 'r');
- if ($handle === false) {
- return $result;
- }
- $keys = api_fgetcsv($handle, null, ';');
- foreach ($keys as $key => &$key_value) {
- $key_value = api_to_system_encoding($key_value, $from_encoding);
- }
- while (($row_tmp = api_fgetcsv($handle, null, ';')) !== false) {
- $row = array();
-
- if (is_array($row_tmp) && count($row_tmp) > 0 && $row_tmp[0] != '') {
- if (!is_null($row_tmp[0])) {
- foreach ($row_tmp as $index => $value) {
- $row[$keys[$index]] = api_to_system_encoding($value, $from_encoding);
- }
- $result[] = $row;
- }
- }
- }
- fclose($handle);
- return $result;
- }
- }
|