I'm having issues printing anything to the PHP page after building a CSV file.
Here is the parsing of input -> array.
<?php
if (isset($_POST['convert'])) {
PrintExcel();
}
function PrintExcel() {
ob_clean();
ob_start();
global $errors, $invalid_input, $finalassembly, $csv_output, $headers, $filename;
$input = $_POST['convert'];
$inputdata = explode("
", $input);
$invalid_input = array();
$csv_output = array();
$filename = date("Y-m-d h:i:sa") . substr(str_shuffle(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ),0, 1) . substr(str_shuffle (aBcEeFgHiJkLmNoPqRstUvWxYz0123456789),0, 31);
$headers = ['Flag'];
foreach ($inputdata as $key => $val) {
$value = str_replace(array("", "
", "
", "\v", "\t", "\0","\x"), " ", $val);
$pos = substr($value,0, strpos($value, ' '));
if (preg_match("[\byes\b|\bno\b]", $val) && (preg_match("enter code here[\bCT\b|\bsi\b]", $val) ===1)){
$usergroup = ",,,,,,,,,,,333/333/xxx/FffffK,";}
else {
$invalid_input[] = "Could not map $pos qualifications to any
usergroup";
continue;
}
if (!empty($pos) && (!empty($usergroup))) {
$finalassembly = "update,".$pos . $usergroup.",";
$csv_output[] = $finalassembly;
} else {
continue;
}
}
file_put_contents('errors.txt', $invalid_input);
$recoveredData = file_get_contents('errors.txt');
$recoveredArray = unserialize($recoveredData);
print_r($recoveredArray);
$empty_input = array_filter($csv_output);
if (!empty($empty_input))
saveCSV();
}
In this function I'm looking for valid input and saving invalid input to an array.. I've tried printing the array, I've tried saving it to a text, then printing it.... but the errors won't print whenever I have ANY valid input data.
When I have valid input data this runs:
function saveCSV() {
global $errors, $finalassembly, $csv_output, $headers, $filename;
ob_end_clean();
$file = fopen('php://memory', "w");
fputcsv($file, $headers);
foreach ($csv_output as $line)
{
fputcsv($file,explode(',',$line));
}
rewind($file);
$csvFile = stream_get_contents($file);
fclose($file);
header('Content-Type: text/csv');
header('Content-Length: '.strlen($csvFile));
header('Content-Disposition: attachment; filename="'.$filename.'.csv"');
exit($csvFile);>?
But none of the errors are printed, and I'm unable to print any of the variables.
Any clues as to why I cannot print anything?
I've tried printing them both before and after calling the saveCSV() function.
You can't send header information after sending any other content
http://php.net/manual/de/function.header.php
if you want to see the errors comment out the header instructions.
Instead of exit() use a simple echo $csvFile.