/** * Read a CSV file and return a PHP array * * @param csv $file * @return PHP array */ function convertCsvFileToArray($file) { ini_set("auto_detect_line_endings", true); //detect mac os line endings too $csvArray = array(); $handle = fopen($file, "r"); $memLimitBytes = toBytes(ini_get("memory_limit")); $memWarningThreshold = WorkbenchConfig::get()->value("memoryUsageWarningThreshold") / 100; $headerCount = 0; for ($row = 0; ($data = fgetcsv($handle)) !== FALSE; $row++) { if ($memLimitBytes != 0 && memory_get_usage() / $memLimitBytes > $memWarningThreshold) { displayError("Workbench almost exhausted all its memory after only processing {$row} rows of data.\n When performing a large data load, it is recommended to use a zipped request for processing with the Bulk API.\n To do so, rename your CSV file to 'request.txt', zip it, and try uploading again to Workbench.", false, true); fclose($handle); return; } if ($row == 0) { $headerCount = count($data); } else { $colCount = count($data); if ($headerCount != $colCount) { fclose($handle); throw new WorkbenchHandledException("Invalid CSV file. All rows must have same number of columns.\n" . "Header contains " . amt($headerCount, "column") . ", but data row {$row} contains " . amt($colCount, "column") . "."); } } for ($col = 0; $col < count($data); $col++) { $csvArray[$row][$col] = $data[$col]; } } fclose($handle); if ($csvArray !== NULL) { return $csvArray; } else { displayError("There were errors parsing the CSV file. Please try again.", false, true); } }
checkTyp($rec = getRec($s), 'T'); echo $flnm . ":<br>\nThis is " . (substr($rec, 27, 1) == 'T' ? 'a <b style="color:orange;">TEST</b> file.' : 'NOT a test file.') . "<br>\n"; checkTyp(getRec($s), 'A'); $tot = 0; while (typ($rec = getRec($s)) == 'B') { $amt = amt($rec); $tot += $amt; //echo "amt=$amt tot=$tot<br>\n"; } checkTyp($rec, 'C'); $amt = amt($rec, FALSE); if ($amt != $tot) { err("bad C total amt={$amt} tot={$tot}"); } checkTyp($rec = getRec($s), 'K'); $amt = amt($rec, FALSE); if ($amt != $tot) { err("bad K total amt={$amt} tot={$tot}"); } echo 'File looks <b style="color:lightgreen;">GOOD</b>! Total = $' . number_format($tot / 100, 2); function checkTyp($rec, $typ) { if (typ($rec) != $typ) { err('missing ' . $typ); } } function typ($rec) { return substr($rec, 0, 1); } function err($err)