Example #1
0
 /**
  * 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
  *   Adam;Adams;adam@mail.com
  *  returns
  *   $result [0]['FirstName'] = 'John';
  *   $result [0]['LastName'] = 'Doe';
  *   $result [0]['Email'] = 'john.doe@mail. com';
  *   $result [1]['FirstName'] = 'Adam';
  *   ...
  * @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.
  * 
  * 
  * @deprecated use cvs_reader instead
  */
 static function csv_to_array($filename, $csv_order = 'vertical')
 {
     $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;
     }
     if ($csv_order == 'vertical') {
         $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();
         // Avoid empty lines in csv
         if (is_array($row_tmp) && count($row_tmp) > 0 && $row_tmp[0] != '') {
             if (!is_null($row_tmp[0])) {
                 if ($csv_order == 'vertical') {
                     foreach ($row_tmp as $index => $value) {
                         $row[$keys[$index]] = api_to_system_encoding($value, $from_encoding);
                     }
                 } else {
                     $first = null;
                     $count = 1;
                     foreach ($row_tmp as $index => $value) {
                         if ($count == 1) {
                             $first = $value;
                         } else {
                             $row[$first][] = api_to_system_encoding($value, $from_encoding);
                         }
                         $count++;
                     }
                 }
                 $result[] = $row;
             }
         }
     }
     fclose($handle);
     return $result;
 }
Example #2
0
/**
 * CSV file import functions
 * @author René Haentjens , Ghent University
 */
function import_csvfile()
{
    global $catlinkstatus;
    // Feedback message to user.
    if (is_uploaded_file($filespec = $_FILES['import_file']['tmp_name']) && filesize($filespec) && ($myFile = @fopen($filespec, 'r'))) {
        // read first line of file (column names) and find ',' or ';'
        $listsep = strpos($colnames = trim(fgets($myFile)), ',') !== false ? ',' : (strpos($colnames, ';') !== false ? ';' : '');
        if ($listsep) {
            $columns = array_map('strtolower', explode($listsep, $colnames));
            if (in_array('url', $columns) && in_array('title', $columns)) {
                $stats = array(0, 0, 0);
                // fails, updates, inserts
                while ($data = api_fgetcsv($myFile, null, $listsep)) {
                    //
                    foreach ($data as $i => &$text) {
                        $linkdata[$columns[$i]] = $text;
                    }
                    $stats[import_link($linkdata)]++;
                    unset($linkdata);
                }
                $catlinkstatus = '';
                if ($stats[0]) {
                    $catlinkstatus .= $stats[0] . ' ' . get_lang('CsvLinesFailed');
                }
                if ($stats[1]) {
                    $catlinkstatus .= $stats[1] . ' ' . get_lang('CsvLinesOld');
                }
                if ($stats[2]) {
                    $catlinkstatus .= $stats[2] . ' ' . get_lang('CsvLinesNew');
                }
            } else {
                $catlinkstatus = get_lang('CsvFileNoURL') . ($colnames ? get_lang('CsvFileLine1') . htmlspecialchars(substr($colnames, 0, 200)) . '...' : '');
            }
        } else {
            $catlinkstatus = get_lang('CsvFileNoSeps') . ($colnames ? get_lang('CsvFileLine1') . htmlspecialchars(substr($colnames, 0, 200)) . '...' : '');
        }
        fclose($myFile);
    } else {
        $catlinkstatus = get_lang('CsvFileNotFound');
    }
}