
MAGENTO 1.X – IMPORTING AND EXPORTING CSV FILES
Many times, you will need to work with external csv’s files. This simple script will help you on import and export them, and other stuff.
- Create a new file called csvworker.php under <MageRoot>/shell/
- If you will not pass the filename through parameter, remember to change method “getFileName()”.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127<?phprequire_once 'abstract.php';ini_set('display_errors', 1);class Offset101_Shell_CSVScript extends Mage_Shell_Abstract{const UPLOAD_DIR = 'path/to/read/files/';const FIELD_1 = 'Column one';const FIELD_2 = 'Column two';/*** Run the process*/public function run(){if ($this->getArg('action') == 'read') $this->readCSVFile();if ($this->getArg('action') == 'write') $this->writeCSVFile();}private function readCSVFile(){$csvObject = new Varien_File_Csv();$fileName = $this->getFileName();if ($this->getArg('formatted')){echo "Reading formatted\n";$csvRows = $csvObject->getData($fileName);$csvRows = $this->getFormattedData($csvRows);foreach ($csvRows as $row) {echo $row[self::FIELD_1] . " " . $row[self::FIELD_2]."\n";}}else{echo "Reading NON formatted\n";$csvRows = $csvObject->getData($fileName);foreach ($csvRows as $row) {echo $row[0]. " ".$row[1]."\n";}}}private function writeCSVFile(){$fileName = $this->getFileName();$destinationCsvFile = fopen($fileName, "w"); // You can use "w+" for read and write.fputcsv($destinationCsvFile, array(self::FIELD_1,self::FIELD_2));fputcsv($destinationCsvFile, array("Value Column1 Row 1","Value Column2 Row 1"));fputcsv($destinationCsvFile, array("Value Column1 Row 2","Value Column2 Row 2"));fclose($destinationCsvFile);}private function getFileName(){if ($fileName = $this->getArg('filename'))return Mage::getBaseDir() . $fileName;elsereturn Mage::getBaseDir() . '/var/<filename>.csv';}/*** Get csv header** @param $rows* @return array*/private function getHeader($rows){$header = array();foreach ($rows[0] as $k => $row) {$header[$k] = $row;}return $header;}/*** Return csv formatted data** Associative array with header csv columns as identifiers.** @param $csv* @return array*/private function getFormattedData($csv){$header = $this->getHeader($csv);$data = array();foreach ($csv as $k => $row) {if ($k == 0) continue;foreach ($header as $i => $column) {$data[$k][$column] = $row[$i];}}return $data;}/*** Retrieve Usage Help Message*/public function usageHelp(){return <<<USAGEUsage: php -f csvworker.php -- [options]--filename <file to process> Show Indexer(s) Status--action <read or write> In order to perform read or write process--formatted Shows a formatted csv using known columnshelp This helpUSAGE;}}$shell = new Offset101_Shell_CSVScript();$shell->run(); - Then, you can call the script on your console, doing the following (under shell forlder):
1234567891011121314# To get HELPphp -f csvworker.php help# Read NON Formatedphp -f csvworker.php -- -action read# Read Formatedphp -f csvworker.php -- -action read -formatted# Write NON Formatedphp -f csvworker.php -- -action write# Write Formatedphp -f csvworker.php -- -action write -formatted
Note: if you just want to save a csv with a content of an array, you can do so, with the following command:
12 $csvObject = new Varien_File_Csv();$csvObject->saveData(Mage::getBaseDir() . '/var/<filename>.csv, $content);