function read_input($filename, $delimiter) { $dialect = new Csv_Dialect(); $dialect->delimiter = $delimiter; try { $reader = new Csv_Reader($filename, $dialect); } catch (Exception $e) { print_error_and_exit('Could not open the specified input file'); } $rows = $reader->toArray(); return $rows; }
/** @todo Luke had this one commented out, figure out why & what to do */ public function test_2_2_Other_Methods_Of_Looping_Through_A_File() { ob_start(); $reader = new Csv_Reader($this->tmpfile); $i = 0; while (($row = $reader->getRow()) && $i < 5) { print $row[1] . "<br>"; $i++; } $captured = ob_get_clean(); $this->assertEquals('Widget<br>Whatsamahoozit<br>Dandy Doodad<br>Thingamajigger<br>Jolly Junk<br>', $captured); }
/** Remove the header, and write the input with predefined column order. */ function prepareTempFile($csvText) { $this->inputFile = sys_get_temp_dir() . '/' . uniqid(); $inputHandle = fopen($this->inputFile, 'w'); fwrite($inputHandle, $csvText); fclose($inputHandle); $this->categoriesFile = sys_get_temp_dir() . '/' . uniqid(); $this->categoriesHandle = fopen($this->categoriesFile, 'w'); $inputReader = new \Csv_Reader($this->inputFile, new \Csv_Dialect()); $i = 0; while ($row = $inputReader->getAssociativeRow()) { $i++; // skip the header if ($i == 1) { continue; } fputcsv($this->categoriesHandle, array('id' => isset($row['id']) ? $row['id'] : 0, 'path' => isset($row['path']) ? $row['path'] : '', 'name' => $row['name'])); } }
function main() { $outputHandle = fopen('sampleProducts.csv', 'w'); $this->seedCsv = fopen('products.csv', 'r'); $fields = $this->fields(); fputcsv($outputHandle, $fields); $seedRow = $this->row(); $newCsv = ''; $newCsv .= implode(',', $fields) . "\n"; $products = new Csv_Reader('ekow-new.csv'); while ($row = $products->getRow()) { $newRow = $seedRow; // images $newRow[11] = '/' . $row[5] . '.jpg'; $newRow[12] = '/' . $row[5] . '.jpg'; $newRow[13] = '/' . $row[5] . '.jpg'; $newRow[self::SKU] = $row[9]; $newRow[self::name] = $row[7]; $newRow[self::description] = $row[8]; $newRow[self::short_description] = $row[8]; $newRow[self::price] = $row[10]; if ($row[4]) { $category = $row[4]; } else { if ($row[3]) { $category = $row[3]; } else { if ($row[2]) { $category = $row[2]; } else { if ($row[1]) { $category = $row[1]; } } } } $newRow[self::category] = $category; fputcsv($outputHandle, $newRow); } echo 'data created to sampleProducts.csv'; }
/** Necessary pre-processing to the import rows, like removing the header, exploding multi-valued strings, etc. */ function preProcessRows() { $inputReader = new \Csv_Reader($this->inputFile, new \Csv_Dialect()); $i = 0; while ($row = $inputReader->getAssociativeRow()) { $i++; // skip the header if ($i == 1) { continue; } fputcsv($this->productHandle, array('sku' => $row['sku'], 'name' => $row['name'], 'active' => isset($row['active']) ? $row['active'] : '', 'base_price' => isset($row['base_price']) ? $row['base_price'] : '', 'attributes' => isset($row['attributes']) ? $row['attributes'] : '')); $categories = $this->explodeCategories($row); foreach ($categories as $category) { fputcsv($this->categoriesHandle, array('product_sku' => $row['sku'], 'category_id' => $category, 'category_name' => '')); } $categoryNames = $this->explodeCategoryNames($row); foreach ($categoryNames as $category) { fputcsv($this->categoriesHandle, array('product_sku' => $row['sku'], 'category_id' => '', 'category_name' => $category)); } } }
public function test_Set_Header() { // the comma-200 file doesn't have a header, so it will be indexed numerically $reader = new Csv_Reader($this->files['comma-200']); $header = array('name', 'date', 'email', 'address_1', 'city', 'state', 'zip', 'country', 'phone', 'fax', 'keywords', 'order_id'); $reader->setHeader($header); $row = $reader->getRow(); $this->assertEqual(array_keys($row), $header); $row = $reader->current(); $this->assertEqual(array_keys($row), $header); $row = $reader->next(); $this->assertEqual(array_keys($row), $header); $allrows = $reader->toArray(); $this->assertEqual(array_keys(current($allrows)), $header); }
function testShouldReadUTF8Text() { $file = sys_get_temp_dir() . '/utf8.csv'; file_put_contents($file, 'foo,ﺡ,bar'); $reader = new Csv_Reader($file, new Csv_Dialect()); $row = $reader->getRow(); $this->assertEquals(array('foo', 'ﺡ', 'bar'), $row); }
set_include_path(realpath('../../') . PATH_SEPARATOR . get_include_path()); ?> <html> <head> <title>Csv Uploader</title> </head> <body> <?php if (!empty($_FILES['csv'])) { require_once 'Csv/Exception/FileNotFound.php'; require_once 'Csv/Reader.php'; $filename = $_FILES['csv']['tmp_name']; try { echo "<table border='1'>"; $reader = new Csv_Reader($filename); $row = $reader->getRow(); echo "<tr>"; foreach ($row as $header) { printf("<th>%s</th>", $header); } echo "</tr>"; while ($row = $reader->getRow()) { echo "<tr>"; foreach ($row as $col) { printf("<td>%s</td>", $col); } echo "</tr>"; } echo "</table>"; } catch (CSv_Exception_FileNotFound $e) {
function testShouldHandleEOFAssociativeRow() { $reader = new Csv_Reader($this->files['tab-200']); $reader->setPosition(201); $this->assertFalse($reader->getAssociativeRow(), 'should handle EOF w/ associative rows'); }
<?php require_once 'vendor/autoload.php'; use GetOptionKit\GetOptionKit; $getopt = new GetOptionKit(); $getopt->add('n|number:=i', 'option requires a integer value'); try { $result = $getopt->parse($argv); $number = $result->number ? $result->number : 10; } catch (Exception $e) { echo 'Try: create.php --number=10'; exit; } $seedFile = 'seed.csv'; $reader = new Csv_Reader($seedFile, new Csv_Dialect()); $headerRow = $reader->getAssociativeRow(); $seedRow = $reader->getAssociativeRow(); $writer = new Csv_Writer(STDOUT, new Csv_Dialect(array('quoting' => Csv_Dialect::QUOTE_ALL))); $writer->writeRow($headerRow); for ($i = 1; $i <= $number; $i++) { $productRow = array_merge($seedRow, array('sku' => 'sku-' . $i, 'name' => 'product ' . $i)); $writer->writeRow($productRow); }