/** * Open stream from a source * A source can be anything: for instance a db, a file, or a socket * * @param String $path * @return void * @throws \Palmabit\Library\Exceptions\CannotOpenFileException */ public function open($path) { $this->convertToLF($path); $this->spl_file_object = new SplFileObject($path); $this->spl_file_object->setCsvControl($this->delimiter); $this->columns_name = $this->spl_file_object->fgetcsv(); }
/** * @param \SplFileObject $file * @param string $delimiter * @param string $enclosure * @param string $escape */ public function __construct(\SplFileObject $file, $delimiter = ',', $enclosure = '"', $escape = '\\') { ini_set('auto_detect_line_endings', true); $this->file = $file; $this->file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY | \SplFileObject::READ_AHEAD | \SplFileObject::DROP_NEW_LINE); $this->file->setCsvControl($delimiter, $enclosure, $escape); }
/** * @return \SplFileObject */ protected function getFile() { if (!$this->file instanceof \SplFileObject) { $this->file = $this->fileInfo->openFile(); $this->file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::DROP_NEW_LINE); $this->file->setCsvControl($this->delimiter, $this->enclosure, $this->escape); if ($this->firstLineIsHeader && !$this->header) { $this->header = $this->file->fgetcsv(); } } return $this->file; }
public function __construct($fileName) { parent::__construct(); $this->fileName = $fileName; try { $this->fileHandle = new \SplFileObject($fileName, 'c+'); $this->fileHandle->setFlags(\SplFileObject::READ_CSV); $this->fileHandle->setCsvControl(self::CSV_DELIMITER, self::CSV_SEPARATOR); } catch (\Exception $exception) { throw new PinqException('Invalid cache file: %s is not readable with the message, "%s"', $fileName, $exception->getMessage()); } $this->fileName = $fileName; }
/** * {@inheritdoc} */ protected function loadResource($resource) { $messages = array(); try { $file = new \SplFileObject($resource, 'rb'); } catch (\RuntimeException $e) { throw new NotFoundResourceException(sprintf('Error opening file "%s".', $resource), 0, $e); } $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY); $file->setCsvControl($this->delimiter, $this->enclosure, $this->escape); foreach ($file as $data) { if (substr($data[0], 0, 1) === '#') { continue; } if (!isset($data[1])) { continue; } if (count($data) == 2) { $messages[$data[0]] = $data[1]; } else { continue; } } return $messages; }
/** * {@inheritdoc} * * @api */ public function load($resource, $locale, $domain = 'messages') { $messages = array(); try { $file = new \SplFileObject($resource, 'rb'); } catch (\RuntimeException $e) { throw new \InvalidArgumentException(sprintf('Error opening file "%s".', $resource)); } $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY); $file->setCsvControl($this->delimiter, $this->enclosure, $this->escape); foreach ($file as $data) { if (substr($data[0], 0, 1) === '#') { continue; } if (!isset($data[1])) { continue; } if (count($data) == 2) { $messages[$data[0]] = $data[1]; } else { continue; } } $catalogue = parent::load($messages, $locale, $domain); $catalogue->addResource(new FileResource($resource)); return $catalogue; }
/** * {@inheritdoc} */ public function load($resource, $locale, $domain = 'messages') { if (!stream_is_local($resource)) { throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); } if (!file_exists($resource)) { throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); } $messages = array(); try { $file = new \SplFileObject($resource, 'rb'); } catch (\RuntimeException $e) { throw new NotFoundResourceException(sprintf('Error opening file "%s".', $resource), 0, $e); } $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY); $file->setCsvControl($this->delimiter, $this->enclosure, $this->escape); foreach ($file as $data) { if ('#' !== substr($data[0], 0, 1) && isset($data[1]) && 2 === count($data)) { $messages[$data[0]] = $data[1]; } } $catalogue = parent::load($messages, $locale, $domain); if (class_exists('Symfony\\Component\\Config\\Resource\\FileResource')) { $catalogue->addResource(new FileResource($resource)); } return $catalogue; }
/** * {@inheritdoc} */ public function read() { if (null === $this->csv) { if (mime_content_type($this->filePath) === 'application/zip') { $this->extractZipArchive(); } $this->csv = new \SplFileObject($this->filePath); $this->csv->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE); $this->csv->setCsvControl($this->delimiter, $this->enclosure, $this->escape); $this->fieldNames = $this->csv->fgetcsv(); } $data = $this->csv->fgetcsv(); if (false !== $data) { if ($data === array(null) || $data === null) { return null; } if ($this->stepExecution) { $this->stepExecution->incrementSummaryInfo('read'); } if (count($this->fieldNames) !== count($data)) { throw new InvalidItemException('pim_base_connector.steps.csv_reader.invalid_item_columns_count', $data, array('%totalColumnsCount%' => count($this->fieldNames), '%itemColumnsCount%' => count($data), '%csvPath%' => $this->csv->getRealPath(), '%lineno%' => $this->csv->key())); } $data = array_combine($this->fieldNames, $data); } else { throw new \RuntimeException('An error occured while reading the csv.'); } return $data; }
public function insert($file, array $callback, $scan_info) { $class = $callback[0]; $method = $callback[1]; $class = Model::factory($class); $this->_handle = fopen($file, 'r'); $headers = fgetcsv($this->_handle, $file); $scan_data = array(); $file = new SplFileObject($file); $file->setFlags(SplFileObject::SKIP_EMPTY); $file->setFlags(SplFileObject::READ_AHEAD); $file->setFlags(SplFileObject::READ_CSV); $file->setCsvControl(",", '"', "\""); $c = 0; foreach ($file as $row) { $c++; if (count($row) === count($headers)) { $scan_data[] = array_combine($headers, $row); $row = array(); } if ($c % $this->insert_threshold == 0) { Logger::msg('info', array('message' => 'flushing ' . $this->insert_threshold . ' rows', "class" => $callback[0], "method" => $callback[1], 'rows_inserted' => $c)); Logger::msg('info', array('memory_usage' => $this->file_size(memory_get_usage()))); $flush = $class->{$method}($scan_data, $scan_info); $scan_data = array(); } } $flush = $class->{$method}($scan_data, $scan_info); $scan_data = array(); Logger::msg('info', array('memory_usage' => $this->file_size(memory_get_usage()))); return $c; }
/** * @param string $code * @param PyStringNode $csv * * @Then /^exported file of "([^"]*)" should contain:$/ * * @throws ExpectationException * @throws \Exception */ public function exportedFileOfShouldContain($code, PyStringNode $csv) { $config = $this->getFixturesContext()->getJobInstance($code)->getRawConfiguration(); $path = $this->getMainContext()->getSubcontext('job')->getJobInstancePath($code); if (!is_file($path)) { throw $this->getMainContext()->createExpectationException(sprintf('File "%s" doesn\'t exist', $path)); } $delimiter = isset($config['delimiter']) ? $config['delimiter'] : ';'; $enclosure = isset($config['enclosure']) ? $config['enclosure'] : '"'; $escape = isset($config['escape']) ? $config['escape'] : '\\'; $csvFile = new \SplFileObject($path); $csvFile->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE); $csvFile->setCsvControl($delimiter, $enclosure, $escape); $expectedLines = []; foreach ($csv->getLines() as $line) { if (!empty($line)) { $expectedLines[] = explode($delimiter, str_replace($enclosure, '', $line)); } } $actualLines = []; while ($data = $csvFile->fgetcsv()) { if (!empty($data)) { $actualLines[] = array_map(function ($item) use($enclosure) { return str_replace($enclosure, '', $item); }, $data); } } $expectedCount = count($expectedLines); $actualCount = count($actualLines); assertSame($expectedCount, $actualCount, sprintf('Expecting to see %d rows, found %d', $expectedCount, $actualCount)); if (md5(json_encode($actualLines[0])) !== md5(json_encode($expectedLines[0]))) { throw new \Exception(sprintf('Header in the file %s does not match expected one: %s', $path, implode(' | ', $actualLines[0]))); } unset($actualLines[0]); unset($expectedLines[0]); foreach ($expectedLines as $expectedLine) { $originalExpectedLine = $expectedLine; $found = false; foreach ($actualLines as $index => $actualLine) { // Order of columns is not ensured // Sorting the line values allows to have two identical lines // with values in different orders sort($expectedLine); sort($actualLine); // Same thing for the rows // Order of the rows is not reliable // So we generate a hash for the current line and ensured that // the generated file contains a line with the same hash if (md5(json_encode($actualLine)) === md5(json_encode($expectedLine))) { $found = true; // Unset line to prevent comparing it twice unset($actualLines[$index]); break; } } if (!$found) { throw new \Exception(sprintf('Could not find a line containing "%s" in %s', implode(' | ', $originalExpectedLine), $path)); } } }
/** * Reads csv records * * @param string $fileName * @param int $position * @param int $step * @return array * @throws \Exception */ public function readRecords($fileName, $position, $step) { $tempFileName = ''; if (file_exists($fileName)) { $tempFileName = $this->uploadPathProvider->getRealPath(md5(microtime() . '.csv')); file_put_contents($tempFileName, file_get_contents($fileName)); $fileName = $tempFileName; } $file = new \SplFileObject($fileName); $file->setCsvControl(";", '"'); $file->setFlags(\SplFileObject::READ_CSV); $columnNames = $this->getColumnNames($file); //moves the file pointer to a certain line // +1 to ignore the first line of the file $file->seek($position + 1); $readRows = []; $data = []; for ($i = 1; $i <= $step; $i++) { $row = $file->current(); if ($this->isInvalidRecord($row)) { break; } foreach ($columnNames as $key => $name) { $data[$name] = isset($row[$key]) ? $row[$key] : ''; } $readRows[] = $data; // Move the pointer to the next line $file->next(); } unlink($tempFileName); return $this->toUtf8($readRows); }
/** * Initialize read process by extracting zip if needed, setting CSV options * and settings field names. */ protected function initializeRead() { if (mime_content_type($this->filePath) === 'application/zip') { $this->extractZipArchive(); } $this->csv = new \SplFileObject($this->filePath); $this->csv->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE); $this->csv->setCsvControl($this->delimiter, $this->enclosure, $this->escape); $this->fieldNames = $this->csv->fgetcsv(); }
/** * Initialize read process by extracting zip if needed, setting CSV options * and settings field names. */ protected function initializeRead() { // TODO mime_content_type is deprecated, use Symfony\Component\HttpFoundation\File\MimeTypeMimeTypeGuesser? if ('application/zip' === mime_content_type($this->filePath)) { $this->extractZipArchive(); } $this->csv = new \SplFileObject($this->filePath); $this->csv->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE); $this->csv->setCsvControl($this->delimiter, $this->enclosure, $this->escape); $this->fieldNames = $this->csv->fgetcsv(); }
public function getRows($chemin) { //jLog::dump($chemin); $csv = new SplFileObject($chemin, 'r'); $csv->setFlags(SplFileObject::READ_CSV); $csv->setCsvControl(','); $rows = array(); $i = 0; foreach ($csv as $ligne) { $rows[$i] = $ligne; $i++; } return $rows; }
/** * * @return array */ private function all($resource) { try { $file = new \SplFileObject($resource, 'rb'); } catch (\RuntimeException $e) { throw new \InvalidArgumentException(sprintf('Error opening file "%s".', $resource)); } $file->setFlags(\SplFileObject::SKIP_EMPTY | \SplFileObject::READ_CSV); $file->setCsvControl(';'); $lines = array(); // iterate over the file's rows // fgets increments file descriptor to next line while ($data = $file->fgetcsv()) { $lines[] = $data; } return $lines; }
public function execute() { $fileIterator = new \SplFileObject($this->sourceFile); $fileIterator->setCsvControl($this->delimitter); $fileIterator->setFlags(\SplFileObject::READ_CSV | \SplFileObject::DROP_NEW_LINE); $columns = []; $data = []; foreach ($fileIterator as $values) { $row = []; foreach ($values as $val) { $row[] = trim($val); } $data[] = $row; } if ($this->columnsFromFirstLine) { $columns = array_shift($data); } $this->connection->getTable($this->tableName)->upsert($data, $columns); }
public function export(TableView $view, $template = null, array $options = array()) { $out = tempnam('/tmp', 'export-out-'); $data = $view->getData(); $file = new \SplFileObject($out, 'w'); $file->setCsvControl($this->delimiter, $this->enclosure, $this->escape); $row = array(); foreach ($data['thead'] as $th) { $row[] = $th['title']; } $file->fputcsv($row); foreach ($data['tbody'] as $tr) { $row = array(); foreach ($tr['data'] as $td) { $row[] = $td['value']; } $file->fputcsv($row); } $now = new \DateTime(); $filename = preg_replace(array('/\\[now\\]/', '/\\[caption\\]/'), array($now->format('Y-m-d H\\hi'), $data['caption']), 'Export'); return new Export($file->getFileInfo(), $this->getContentType(), $filename, $this->getFileExtension()); }
public function getCsvFile() { // No path provided, let's assume a fixed directory if ($this->cli->options['path'] === null) { $geonamesPath = $this->getDefaultGeonamesPath(); } else { $geonamesPath = rtrim($this->cli->options['path'], DS) . DS; } // Combine a path with a filename $geonamesFileName = $geonamesPath . mb_strtoupper($this->cli->args['language']) . '.txt'; // Create file object try { $file = new SplFileObject($geonamesFileName); $file->setFlags(SplFileObject::READ_CSV | SplFileObject::SKIP_EMPTY); $file->setCsvControl("\t"); } catch (RuntimeException $e) { printf("An error ocurred: %s\n" , $e->getMessage()); exit($e->getCode()); } return $file; }
public function importGetFileInfoAction() { $success = true; $supportedFieldTypes = array("checkbox", "country", "date", "datetime", "href", "image", "input", "language", "table", "multiselect", "numeric", "password", "select", "slider", "textarea", "wysiwyg", "objects", "multihref", "geopoint", "geopolygon", "geobounds", "link", "user", "email", "gender", "firstname", "lastname", "newsletterActive", "newsletterConfirmed", "countrymultiselect", "objectsMetadata"); $file = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_" . $this->getParam("id"); // determine type $dialect = Tool\Admin::determineCsvDialect(PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_" . $this->getParam("id") . "_original"); $count = 0; if (($handle = fopen($file, "r")) !== false) { while (($rowData = fgetcsv($handle, 0, $dialect->delimiter, $dialect->quotechar, $dialect->escapechar)) !== false) { if ($count == 0) { $firstRowData = $rowData; } $tmpData = array(); foreach ($rowData as $key => $value) { $tmpData["field_" . $key] = $value; } $data[] = $tmpData; $cols = count($rowData); $count++; if ($count > 18) { break; } } fclose($handle); } // get class data $class = Object\ClassDefinition::getById($this->getParam("classId")); $fields = $class->getFieldDefinitions(); $availableFields = array(); foreach ($fields as $key => $field) { $config = null; $title = $field->getName(); if (method_exists($field, "getTitle")) { if ($field->getTitle()) { $title = $field->getTitle(); } } if (in_array($field->getFieldType(), $supportedFieldTypes)) { $availableFields[] = array($field->getName(), $title . "(" . $field->getFieldType() . ")"); } } $mappingStore = array(); for ($i = 0; $i < $cols; $i++) { $mappedField = null; if ($availableFields[$i]) { $mappedField = $availableFields[$i][0]; } $firstRow = $i; if (is_array($firstRowData)) { $firstRow = $firstRowData[$i]; if (strlen($firstRow) > 40) { $firstRow = substr($firstRow, 0, 40) . "..."; } } $mappingStore[] = array("source" => $i, "firstRow" => $firstRow, "target" => $mappedField); } //How many rows $csv = new SplFileObject($file); $csv->setFlags(SplFileObject::READ_CSV); $csv->setCsvControl($dialect->delimiter, $dialect->quotechar, $dialect->escapechar); $rows = 0; $nbFields = 0; foreach ($csv as $fields) { if (0 === $rows) { $nbFields = count($fields); $rows++; } elseif ($nbFields == count($fields)) { $rows++; } } $this->_helper->json(array("success" => $success, "dataPreview" => $data, "dataFields" => array_keys($data[0]), "targetFields" => $availableFields, "mappingStore" => $mappingStore, "rows" => $rows, "cols" => $cols)); }
private function getLines($source) { $result = null; if (is_string($source)) { if ($this->isFile($source)) { $result = new \SplFileObject($source); } elseif (is_string($source)) { $result = new \SplFileObject('data://text/csv;base64,' . base64_encode($source)); } $result->setFlags(\SplFileObject::READ_CSV); $result->setCsvControl($this->_delimiter, $this->_enclosure, $this->_escape); } elseif (is_array($source)) { $result = new \ArrayIterator($source); } elseif ($source instanceof \Traversable) { $result = $source; } return empty($result) ? NULL : $result; }
<?php file_put_contents('csv_control_data.csv', <<<CDATA 'groene appelen'|10 'gele bananen'|20 'rode kersen'|30 CDATA ); $s = new SplFileObject('csv_control_data.csv'); $s->setFlags(SplFileObject::READ_CSV); $s->setCsvControl('|', '\'', 'three'); error_reporting(0); unlink('csv_control_data.csv');
<?php file_put_contents('csv_control_data.csv', <<<CDATA 'groene appelen'|10 'gele bananen'|20 'rode kersen'|30 CDATA ); $s = new SplFileObject('csv_control_data.csv'); $s->setFlags(SplFileObject::READ_CSV); $s->setCsvControl('|', '\'', '/'); foreach ($s as $row) { list($fruit, $quantity) = $row; echo "{$fruit} : {$quantity}\n"; } error_reporting(0); unlink('csv_control_data.csv');
/** * */ function save() { $rep = $this->getResponse('json'); if (isset($_FILES['candidats'])) { $this->success = true; //get files $file_name = 'liste_candidats_' . uniqid(); //$csv = new SplFileObject($this->saveFile($_FILES['candidats'],$file_name), 'r'); $csv = new SplFileObject($_FILES['candidats']['tmp_name'], 'r'); $csv->setFlags(SplFileObject::READ_CSV); $csv->setCsvControl(';'); $rows = array(); $i = 0; foreach ($csv as $ligne) { $rows[$i] = $ligne; $i++; } // get header ( colonnes) : fields $indexMatricule = array_search('matricule', $rows[0]); jLog::dump($indexMatricule); $indexDdn = array_search('ddn', $rows[0]); $indexLn = array_search('ln', $rows[0]); $indexConcour = array_search('concour', $rows[0]); $indexCodebarre = array_search('codebarre', $rows[0]); $indexNom = array_search('nom', $rows[0]); $indexPrenom = array_search('prenom', $rows[0]); $indexDatevisite = array_search('datevisite', $rows[0]); // instanciation de la factory $tb = jDao::get("candidat"); $cnx = jDb::getConnection(); $i = 0; set_time_limit(60 * 60); foreach ($csv as $ligne) { if ($i > 0) { try { $cnx->beginTransaction(); //i start la transaction $record = jDao::createRecord("candidat"); // on remplit le record $record->matricule = $ligne[$indexMatricule]; $record->ddn = $ligne[$indexDdn]; $record->ln = $ligne[$indexLn]; $record->concour = $ligne[$indexConcour]; $record->codedarre = $ligne[$indexCodebarre]; $record->nom = $ligne[$indexNom]; $record->prenom = $ligne[$indexPrenom]; $record->datevisite = $ligne[$indexDatevisite]; // on le sauvegarde dans la base $tb->insert($record); $cnx->commit(); } catch (Exception $e) { $cnx->rollback(); $this->msg = "Des doublons ont été detectés dans le fichier source et ignorés lors de l'operation"; } } $i++; } } $rep->data = array('success' => $this->success, 'msg' => $this->msg); return $rep; }
private function getErrorsByType(array $types = array(self::ERROR, self::WARNING)) { $result = array(); if (!$this->getEnabled() || !file_exists($this->getPath())) { return $result; } $delimiter = ","; $file = new \SplFileObject($this->getPath(), 'r'); $file->setFlags(\SplFileObject::READ_CSV); $file->setCsvControl($delimiter); while (!$file->eof()) { $current = $file->current(); $current = array_filter($current); if (!empty($current) && isset($current[2]) && in_array($current[2], $types)) { $result[] = $current; } $file->next(); } return $result; }
public function scanDirectoriesForTranslations() { $iterator = new \DirectoryIterator($this->getTranslationDirectories()); foreach ($iterator as $info) { if ($info->isDot()) { continue; } if ($info->isFile() && $info->isReadable()) { $file = new \SplFileObject($info->getRealPath()); $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY); $file->setCsvControl(';', '"'); foreach ($file as $row) { if (!isset($row[0]) || !isset($row[1]) || strpos($row[0], '//') === 0 || strpos($row[0], '/*') === 0 || strpos($row[0], '#') === 0) { continue; } self::$_translations[$locale][$row[0]] = $row[1]; } } } }
/** * Get raw file object * * @param string $path File path * * @return \SplFileObject */ protected function getRawFile($path) { $sfo = new \SplFileObject($path, 'rb'); $sfo->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY); $sfo->setCsvControl($this->importer->getOptions()->delimiter, $this->importer->getOptions()->enclosure); return $sfo; }
<?php /** * */ $file = new SplFileObject('./phpcon2010.tsv'); $file->setFlags(SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY | SplFileObject::READ_CSV); $file->setCsvControl("\t"); foreach ($file as $line) { /* array(10) { [0]=> string(11) "25080858457" [1]=> string(31) "Tue, 21 Sep 2010 02:18:12 +0000" [2]=> string(6) "hagimi" [3]=> string(6) "348326" [4]=> string(2) "ja" [5]=> string(70) "http://a1.twimg.com/profile_images/1090295913/IMG_0247_mini_normal.jpg" [6]=> string(70) "http://a1.twimg.com/profile_images/1090295913/IMG_0247_mini_normal.jpg" [7]=> string(59) "<a href="http://twitter.com/">web</a>" [8]=> string(190) "RT @phpcon: いよいよ、PHPカンファレンスまで1週間となりました。参加登録をしていない方はおはやめに! http://phpcon.php.gr.jp/2010/register/ #phpcon2010" [9]=> string(4) "null"
<?php file_put_contents('SplFileObject_setCsvControl_variation001.csv', <<<CDATA "groene appelen",10 "gele bananen",20 "rode kersen",30 CDATA ); $s = new SplFileObject('SplFileObject_setCsvControl_variation001.csv'); $s->setFlags(SplFileObject::READ_CSV); $s->setCsvControl(); foreach ($s as $row) { list($fruit, $quantity) = $row; echo "{$fruit} : {$quantity}\n"; } unlink('SplFileObject_setCsvControl_variation001.csv');
<?php file_put_contents('csv_control_data.csv', <<<CDATA 'groene appelen'|10 'gele bananen'|20 'rode kersen'|30 CDATA ); $s = new SplFileObject('csv_control_data.csv'); $s->setFlags(SplFileObject::READ_CSV); $s->setCsvControl('|', 'two');
public static function configureFileObject(\SplFileObject $fh, Dialect $dialect) { $fh->setCsvControl($dialect->getDelimiter(), $dialect->getEnclosure(), $dialect->getEscapeCharacter()); }