$csv_type = $_FILES['election_csv']['type']; $csv_size = $_FILES['election_csv']['size']; $csv_tmp = $_FILES['election_csv']['tmp_name']; $csv_ext = strtolower(end(explode('.', $csv_name))); $csv_valid_types = array('text/csv', 'application/csv', 'text/comma-separated-values', 'application/excel', 'application/vnd.ms-excel', 'application/vnd.msexcel', 'application/octet-stream'); $target = CSV_PATH . basename($csv_name); if (!is_uploaded_file($csv_tmp)) { $errors[] = 'Please upload CSV file'; } elseif ($csv_size > MAX_FILE_SIZE) { $errors[] = 'The CSV file must not be greater than ' . MAX_FILE_SIZE / 1024 . 'KB'; } elseif (!in_array($csv_type, $csv_valid_types) || $csv_ext !== 'csv') { $errors[] = 'Uploaded file must be in the CSV format'; } elseif (!move_uploaded_file($csv_tmp, $target)) { $errors[] = 'There was problem uploading your csv file'; } $csvFields = readCsv($target); $field_count = 0; if (!$csvFields) { $errors[0] = 'Cannot read csv file. Please upload a valid csv file'; } else { foreach ($csvFields as $field) { $field_count = count($field); } if ($field_count != 1) { $errors[] = 'Please upload a csv file containing emails only'; } $emails = array_values_recursive($csvFields); $valid_email_count = 0; if (count($emails) == 0) { $errors[] = 'The uploaded csv file contains no valid email address'; } else {
/** * Reads a CSV file in as an associative array using the header names as array * keys. Contents are converted to UTF-8, the row lengths are normalized, and * two padding rows are used at the top to ensure that row indices correspond * to matching Excel rows. * @param string $filename The file to read. * @param bool $padding Set false if no padding rows are desired. * @param string $delimiter A single character noting the delimiter in the file. * @return array */ function GetFromFile($filename, $padding = true, $delimiter = ",") { // make sure PHP auto-detects line endings ini_set('auto_detect_line_endings', true); // read the file in and get the header $dataDirty = readCsv($filename, $delimiter); $data = trimArrayRecursive($dataDirty); $data = convertArrayEncodingRecursive($data, 'UTF-8'); $columns = array_shift($data); $columnsCount = count($columns); // make first two indices blank so that others correspond to Excel rows // build the rest of the output array $out = $padding == true ? array(0 => 0, 1 => 0) : array(); foreach ($data as $row) { // add values to row if there are more columns than values for ($rowCount = count($row); $columnsCount > $rowCount; $rowCount++) { $row[] = ''; } // trim values from row if there are fewer columns than values if ($columnsCount < $rowCount) { $row = array_slice($row, 0, $columnsCount); } // convert to column=>value pairs and add to output if (!isBlankLine($row)) { $out[] = array_combine($columns, $row); } } return $out; }
<?php require 'insuranceLogic.php'; $plans = readCsv($argv[1]); $familyMembers = readCsv($argv[2]); writeTitle(); foreach ($plans as &$plan) { $plan['Total Premium'] = calculatePremium($plan, $familyMembers); $plan['Total Extra'] = calculateExtra($plan, $familyMembers); $plan['Total Copay'] = calculateCopay($plan, $familyMembers); $plan['Total'] = $plan['Total Premium'] * 12 + $plan['Total Extra'] + $plan['Total Copay']; } function cmp($a, $b) { if ($a['Total'] == $b['Total']) { return 0; } return $a['Total'] < $b['Total'] ? -1 : 1; } usort($plans, "cmp"); foreach ($plans as $plan) { writePlan($plan); }
$username = $details['username']; $password = $details['password']; $url = $details['url']; } //Create cluster if (isset($_POST['submit'])) { //if no cluster exists, create one if ($_SESSION['clusterUp'] == 'false') { $success = create_cluster($nodes, $nodeSizeM, $nodeSize); //if a string is not found, there was an error if ($success == false) { $_SESSION['clusterUp'] = 'error'; } elseif ($success == true) { $_SESSION['clusterUp'] = 'true'; //Get access details $readCsv = readCsv($nodes); $details = getAccessDetails(); $username = $details['username']; $password = $details['password']; $url = $details['url']; } } } //Tear it down if (isset($_POST['kubeDown'])) { $old_path = getcwd(); chdir('/var/www/kubernetes'); $a = popen('sh down_cluster.sh', 'r'); while ($b = fgets($a, 2048)) { echo "<p style='color:#337ab7; font-size: 12px;'>.{$b}.</p>" . "<br>\n"; ob_flush();
$procData = readCsv("Experiments/{$experiment}/Procedures/{$procFile}.csv"); // TODO: validate proc data here - actually, move this to the JS if ($procData === false) { trigger_error("Procedure file '{$procFile}' failed to load", E_USER_ERROR); } if (!isset($procData[0]['Type'])) { trigger_error("Procedure file '{$procFile}' is missed a 'Type' column, which is required", E_USER_ERROR); } // TODO: shuffle proc data here foreach ($procData as $row) { $expData['Procedure'][] = $row; } } # LOAD STIM foreach ($files['Stimuli'] as $i => $stimFile) { $stimData = readCsv("Experiments/{$experiment}/Stimuli/{$stimFile}.csv"); // TODO: validate stim data here - actually, move this to the JS if ($stimData === false) { trigger_error("Procedure file '{$procFile}' failed to load", E_USER_ERROR); } // TODO: shuffle stim data here $expData['Stimuli'][$i] = $stimData; } #### CREATE EXPERIMENT FILE // create js file // file names defined at the top of Experiment.php $expData = 'var expData = ' . json_encode($expData) . ";\n"; // end on a newline $expFileDir = dirname($expFile); if (!is_dir($expFileDir)) { mkdir($expFileDir, 0777, true);