/** * Return or create the file descriptor associated with a file * * @param string $file The name of the file * @param array &$conf The configuration * @param string $mode The open node (ex: FILE_MODE_READ or FILE_MODE_WRITE) * * @return mixed A file resource or false */ function getPointer($file, &$conf, $mode = FILE_MODE_READ) { static $resources = array(); static $config; if (isset($resources[$file])) { $conf = $config; return $resources[$file]; } File_CSV::_conf($conf, $error); if ($error) { return File_CSV::raiseError($error); } $config = $conf; PEAR::pushErrorHandling(PEAR_ERROR_RETURN); $fp =& File::_getFilePointer($file, $mode); PEAR::popErrorHandling(); if (PEAR::isError($fp)) { return File_CSV::raiseError($fp); } $resources[$file] = $fp; if ($mode == FILE_MODE_READ && !empty($conf['header'])) { if (!File_CSV::read($file, $conf)) { return false; } } return $fp; }
require_once 'Log.php'; require_once 'File/CSV.php'; require_once $ClassDir . 'StringHelper.class.php'; $PattenDataDir = "data/"; $conf = array('fields' => 15, 'sep' => ";", 'quote' => '"', 'header' => false, 'crlf' => "\r\n"); // read data //2006_10_19_02_03.txt //2006_10_19_05_56.txt //2006_10_19_07_10.txt //2006_10_20_08_42.txt //2006_10_20_12_42.txt //2006_10_20_16_49.txt //2006_10_25_11_10.txt $filename = "D:/data_temp/ÆóÒµÊý¾Ý/2006_10_20_16_49.txt"; $x = 1; while ($fields = File_CSV::read($filename, $conf)) { $apf_company = DB_DataObject::factory('ApfCompany'); $apf_company->setName($fields[0]); $apf_company->setAddrees($fields[1]); $apf_company->setPhone($fields[2]); $apf_company->setFax($fields[3]); $apf_company->setHomepage($fields[5]); $apf_company->setLinkMan($fields[8]); $apf_company->setMemo($fields[14]); $apf_company->setName($fields[0]); $apf_company->setAddrees($fields[1]); $apf_company->setPhone($fields[2]); $apf_company->setFax($fields[3]); $apf_company->setEmail($fields[4]); $apf_company->setHomepage($fields[5]); $apf_company->setEmployee($fields[6]);
$csv_format_column[$val['csv_format_column_number']] = $val['csv_column_id']; } } } // read uploaded file if ($action == 'save') { if (isset($_FILES['file']) && $_FILES['file']['size'] > 0) { $tempfile = DIR_FS_CATALOG . '/temp/csv_format_' . date('YmdHis'); $_SESSION['product_csv']['upfile'] = $tempfile; move_uploaded_file($_FILES['file']['tmp_name'], $tempfile); } if (is_readable($_SESSION['product_csv']['upfile'])) { $tempfile = $_SESSION['product_csv']['upfile']; $conf = File_CSV::discoverFormat($tempfile); File_CSV::getPointer($tempfile, $conf); $data = File_CSV::read($tempfile, $conf); } if ($data != false) { $hidden_fields .= zen_draw_hidden_field('csv_format_name', $csv_format_name); $disable_format_name = ' disabled="disabled"'; $name_csv_format_name = 'current_csv_format_name'; $setting_new = '<table border="1">'; $setting_new .= '<tr><th>' . FORM_FORMAT_COLUMN_HEADER1 . '</th><th>' . FORM_FORMAT_COLUMN_HEADER2 . '</th><th>' . FORM_FORMAT_COLUMN_HEADER3 . '</th></tr>'; $count = 0; foreach ($data as $val) { $count++; $setting_new .= ' <tr><td>' . sprintf(FORM_FORMAT_COLUMN_NAME, $count) . '</td><td>' . mb_convert_encoding($val, MODULE_PRODUCT_CSV_INTERNAL_CHARACTER, 'auto') . '</td><td>' . zen_draw_pull_down_menu('csv_format_column[' . $count . ']', $format_columns, isset($csv_format_column) ? $csv_format_column[$count] : $format_columns[$count - 1]['id']); } $setting_new .= '</table>'; switch ($csv_format_type_id) {
/** * 連想配列の取得 * * <pre> * PHPの連想配列を指定すればそのまま、ファイルパスを指定すれば * 設定ファイルから読み込み連想配列として渡します。 * またURLのクエリー形式も使用できます。 * BEARで広く使われています。BEARの全てのクラスのコンストラクタ * (シングルトン含む)、リソースへの引数、 * オプションにこの連想配列フォーマットが使われます。 * * array -- 連想配列としてオプションが入力されます * * string -- ファイルから拡張子によりフォーマットが異なります * URLクエリー形式 ?foo1=bar1&hoge=fugaのようなフォーマットを連想配列にします * *.ini iniフォーマット * *.xml XMLフォーマット * *.php phpのdefineが連想配列として読み込まれます * *.yml yamlファイル * * $options * 'extention' string オーバーロード拡張子 * </pre> * * @param mixed $target ターゲット ファイルパス,URLなど * @param array $options オプション * * @return array * * @see http://pear.php.net/manual/ja/package.configuration.config.php * @see BEAR/test/files/example.ini * @see BEAR/test/files/example.xml * @see BEAR/test/files/example.php * @throws BEAR_Exception */ public static function loadValues($target, $options = array()) { if (!is_file((string) $target)) { // arrayならそのまま if (is_array($target) || is_object($target)) { return (array) $target; } // false | null なら 設定な if (!$target) { return null; } // クエリーがあるときはクエリーをパースした連想配列を返す $parseUrl = parse_url($target); if (isset($parseUrl['query'])) { $options = array(); parse_str($parseUrl['query'], $options); return $options; } else { return null; } } else { $cache = self::factory('BEAR_Cache'); $cache->setLife(BEAR_Cache::LIFE_UNLIMITED); $key = $target . filemtime($target); $cacheResult = $cache->get($key); if ($cacheResult) { return $cacheResult; } // PEAR::Configを使って設定ファイルをパース $pathinfo = pathinfo($target); // 相対パスなら絶対パスに (/:linux :win) $target = substr($target, 0, 1) == '/' || substr($target, 1, 1) == ':' ? $target : _BEAR_APP_HOME . '/App/Ro/' . $target; $extension = isset($options['extention']) ? $options['extention'] : $pathinfo['extension']; switch ($extension) { case 'yml': if (function_exists('syck_load')) { $content = file_get_contents($target); $yaml = syck_load($content); } else { include_once 'BEAR/vendors/spyc-0.2.5/spyc.php'; $yaml = Spyc::YAMLLoad($target); } $cache->set($key, $yaml); return $yaml; case 'csv': $conf = File_CSV::discoverFormat($target); $csv = array(); while ($fields = File_CSV::read($target, $conf)) { array_push($csv, $fields); } $result = $cache->set($key, $csv); return $csv; case 'ini': $parse = 'inicommented'; break; case 'xml': $unserializer = new XML_Unserializer(); $unserializer->setOption('parseAttributes', true); $xml = file_get_contents($target); $unserializer->unserialize($xml); $result = $unserializer->getUnserializedData(); return $result; break; case 'php': $parse = 'PHPConstants'; break; default: return file_get_contents($target, FILE_TEXT); break; } $config = new Config(); $root =& $config->parseConfig($target, $parse); if (PEAR::isError($root)) { $msg = '設定を読み込む際のエラー: '; $msg .= $root->getMessage(); $info = array('parse' => $parse, 'input' => $target); throw new BEAR_Exception($msg, compact('info')); return false; } else { $result = $root->toArray(); return $result['root']; } } }
function import($tempfile, $csv_format_type_id, $ignore_first_line, $unlink = true) { global $ProductCSV; global $format; global $data; global $messageStack; global $line_count; global $success_count; global $error_count; global $missing_count; global $body; $format = $ProductCSV->getFormatById($csv_format_type_id); $conf = File_CSV::DiscoverFormat($tempfile); File_CSV::getPointer($tempfile, $conf); // check format if ($conf['fields'] == count($format['columns'])) { $count = 1; // skip first line if ($ignore_first_line) { File_CSV::read($tempfile, $conf); $count++; } // output table header $body = '<tr><td><table border="1">'; $body .= '<tr>'; $body .= '<th>' . PRODUCT_CSV_TABLE_LINE_NUMBER . '</th>'; foreach ($format['columns'] as $val) { $body .= '<th>' . $val['csv_column_name'] . '</th>'; } $body .= '<th>' . PRODUCT_CSV_TABLE_HEADER . '</th>'; $body .= '</tr>'; // init count $line_count = 0; $success_count = 0; $error_count = 0; $missing_count = 0; // read lines while (($data = File_CSV::read($tempfile, $conf)) !== false) { if (count($data) == 0) { continue; } echo ' '; flush(); // convert charactr set to internal encoding foreach ($data as $key => $val) { $data[$key] = mb_convert_encoding($val, MODULE_PRODUCT_CSV_INTERNAL_CHARACTER, MODULE_PRODUCT_CSV_IMPORT_CHARACTER); } $ProductCSV->messageStack->reset(); switch ($format['csv_format_type_id']) { case 1: $import_status = $ProductCSV->importProduct($data, $format); break; case 2: $format = zen_add_number_to_format($format); $import_status = $ProductCSV->importCategory($data, $format); break; case 3: $import_status = $ProductCSV->importOption($data, $format); break; } // store success/error/line count $line_count++; if ($import_status === true) { $success_count++; } elseif ($import_status === false) { $error_count++; } else { $missing_count++; } $body .= '<tr>'; $body .= '<td>' . $count++ . '</td>'; foreach ($data as $val) { if ($val == '') { $val = ' '; } $body .= '<td>' . $val . '</td>'; } $body .= '<td>' . $ProductCSV->messageStack->output() . '</td>'; $body .= '</tr>'; } $body .= '</table></td></tr>'; $messageStack->add(sprintf(PRODUCT_CSV_MESSAGE_IMPORT_STATUS, $line_count, $success_count, $error_count), 'success'); if ($unlink) { unlink($tempfile); } } else { $messageStack->add(PRODUCT_CSV_ERROR_INVALID_FORMAT, 'caution'); } }
<?php /** * var_export(get_class_methods('File_CSV')); * * array ( * 0 => 'raiseError', * 1 => '_conf', * 2 => 'getPointer', * 3 => 'unquote', * 4 => 'readQuoted', * 5 => 'read', * 6 => '_dbgBuff', * 7 => 'write', * 8 => 'discoverFormat', * 9 => 'resetPointer', * ) */ require_once 'File/CSV.php'; $csv = new File_CSV(); $file = 'tests/data/symmetric.csv'; $conf = $csv->discoverFormat($file); foreach (range(0, (int) $conf['fields']) as $iteration) { if ($iteration == 0) { $x = $csv->read($file, $conf); var_export($x); } // rows }