Feature #1834 - Adding encoding detection and encoding conversion functionality in Import::csv_to_array().

skala
Ivan Tcholakov 15 years ago
parent b9e8cd0f8e
commit 684680cac0
  1. 4
      main/admin/user_import.php
  2. 59
      main/inc/lib/import.lib.php

@ -320,8 +320,6 @@ if ($_POST['formSent'] AND $_FILES['import_file']['size'] !== 0) {
if (is_array($users)) {
foreach ($users as $my_user) {
if (!in_array($my_user['UserName'], $user_id_error)) {
$my_user['FirstName'] = api_to_system_encoding($my_user['FirstName']);
$my_user['LastName'] = api_to_system_encoding($my_user['LastName']);
$users_to_insert[] = $my_user;
}
}
@ -330,7 +328,7 @@ if ($_POST['formSent'] AND $_FILES['import_file']['size'] !== 0) {
$inserted_in_course = array();
// this replace if (strcmp($_FILES['import_file']['type'], 'text/'.$file_type.'') === 0)
if (strcmp($file_type, 'csv') === 0) {
save_data($users_to_insert);
} elseif (strcmp($file_type, 'xml') === 0) {
save_data($users_to_insert);

@ -1,17 +1,18 @@
<?php
/* For licensing terms, see /license.txt */
/**
==============================================================================
* This class provides some functions which can be used when importing data from
* external files into Dokeos
* @package dokeos.library
==============================================================================
*/
class Import
{
* This class provides some functions which can be used when importing data from
* external files into Chamilo.
* @package chamilo.library
*/
class Import {
/**
* Reads a CSV-file into an array. The first line of the CSV-file should
* contain the array-keys.
* Reads a CSV-file into an array. The first line of the CSV-file should contain the array-keys.
* The encoding of the input file is tried to be detected.
* The elements of the returned array are encoded in the system encoding.
* Example:
* FirstName;LastName;Email
* John;Doe;john.doe@mail.com
@ -22,29 +23,47 @@ class Import
* $result [0]['Email'] = 'john.doe@mail. com';
* $result [1]['FirstName'] = 'Adam';
* ...
* @param string $filename Path to the CSV-file which should be imported
* @return array An array with all data from the CSV-file
* @param string $filename The path to the CSV-file which should be imported.
* @return array Returns an array (in the system encoding) that contains all data from the CSV-file.
*/
function csv_to_array($filename) {
$result = array();
// Encoding detection.
$handle = fopen($filename, 'r');
if ($handle === false) {
return $result;
}
$buffer = array();
$i = 0;
while (!feof($handle) && $i < 200) {
// We assume that 200 lines are enough for encoding detection.
$buffer[] = fgets($handle);
$i++;
}
fclose($handle);
$buffer = implode("\n", $buffer);
$from_encoding = api_detect_encoding($buffer);
unset($buffer);
// Reading the file, parsing and importing csv data.
$handle = fopen($filename, 'r');
if ($handle === false) {
return $result;
}
// Modified by Ivan Tcholakov, 01-FEB-2010.
//$keys = fgetcsv($handle, 4096, ";");
$keys = api_fgetcsv($handle, null, ';');
//
// Modified by Ivan Tcholakov, 01-FEB-2010.
//while (($row_tmp = fgetcsv($handle, 4096, ";")) !== FALSE) {
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();
//avoid empty lines in csv
// Avoid empty lines in csv.
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]] = $value;
$row[$keys[$index]] = api_to_system_encoding($value, $from_encoding);
}
$result[] = $row;
}

Loading…
Cancel
Save