Why PHP continue to output to CSV file after fclose()? -
on web page, writing data csv file using below code , closing fclose();
header('content-type: text/csv; charset=utf-8'); header('content-disposition: attachment; filename='.$filename); $out = fopen('php://output', 'w'); fputcsv($out, $cvs_cols); fclose($out); echo "hello world"; // sneaks csv!?
why "hello world" gets csv download file when has fclose()? want output rest of html page displayed in browser. how can that?
after 1 http request follows 1 response. cannot send content type text/csv
, content type text/html
@ same time (maybe yes spdy, not pure http).
fclose
closes file descriptor not output browser.
you should set content-length
header , put in filesize.
mark baker gave important point in comments:
echo
, writing php://output
puts content same stream: stdout
. other options write csv memory (but senseless if don't use it) or file. read more streams: http://www.php.net/manual/en/features.commandline.io-streams.php
possible solution:
you need 2 http requests. 1 download, other html. popular way first use html response , put in like
<meta http-equiv="refresh" content="3; url=http://yourserver.com/download.php?id=pdf&id=123" />
this starts download after 3 seconds.
Comments
Post a Comment