file io - PHP: headers already sent when using fwrite but not when using fputcsv -
i know theory behind error driving me crazy again. i'm using tonic in application. library redirect traffic dispatch.php
script executes appropriate resource
, resource
return response
displayed (output) dispatch.php
.
the output method of response
this:
/** * output response */ public function output() { foreach ($this->headers $name => $value) { header($name.': '.$value, true, $this->responsecode()); } echo $this->body; }
so afaik tells can't write php output in resource
.
i have resource
dynamically generates csv input csv , outputs browser (it converts 1 column of data different format).
$csv = fopen('php://output', 'w'); // sets header $response->__set('contentdisposition:', 'attachment; filename="' . $filename . '"'); while (($line = fgetcsv($filepointer, 0, ",", '"')) !== false) { // generate line fputcsv($csv, $line); } fclose($filepointer); return $response;
this works 100% fine. no issue headers , correct file generated , offered download. confusing because writing output before headers set? fputcsv do?
i have second resource similar thing outputs custom file format (text file).
$output = fopen('php://output', 'w'); // sets header $response->__set('contentdisposition:', 'attachment; filename="' . $filename . '"'); while (($line = fgetcsv($filepointer, 0, ",", '"')) !== false) { // generate record (multiple lines) not shown / snipped fwrite($output, $record); } fclose($filepointer); return $response;
the difference uses fwrite instead of fputcsv , bang
headers sent by... // line number = fwrite()
this confusing! imho should fail in both cases? why first 1 work? how can second 1 work? (i can generate huge string containing file , put responses body , works. files rather big (up 50 mb) , hence want avoid this.)
$record
not set, generating error of level notice
. if have error_reporting
true
, php put error in output before sending headers.
set error_reporting
false , keep eye on logs instead.
Comments
Post a Comment