-
yoshi Junior Member
 
-
Posts: 45
Threads: 18
Joined: Sep 2021
Reputation:
1
hello
this is simple csv. I try output the csv.
PHP Code: public function csvExport(): string { // load from user table into entity $user = [ ['id' => 1, 'name' => 'Mr. A', 'email' => '[email protected]', 'password' => 'abcba'], ['id' => 2, 'name' => 'Mr. B', 'email' => '[email protected]', 'password' => 'bcdcb'], ['id' => 3, 'name' => 'Mr. C', 'email' => '[email protected]', 'password' => 'cdedc'], ]; // load from grades table into entity $grades = [ ['id' => 1, 'name' => 'Mr. A', 'score1' => 100, 'score2' => 70], ['id' => 2, 'name' => 'Mr. B', 'score1' => 99, 'score2' => 50], ['id' => 3, 'name' => 'Mr. C', 'score1' => 29, 'score2' => 100], ]; // mixing entities // ... $header = ['id', 'name', 'email', 'password', 'score1', 'score2']; $csvFileName = 'sample.csv'; // TYPE1 header('Content-Type: application/octet-stream'); header("Content-Disposition: attachment; filename=$csvFileName"); // open stream $stream = fopen('php://output', 'w'); // UTF-8 BOM for excel open. fputs($stream, (chr(0xEF) . chr(0xBB) . chr(0xBF))); // write header and data fputcsv($stream, $header, ',', '"'); foreach ($user as $row) { fputcsv($stream, $row, ',', '"'); } // close stream fclose($stream); // TYPE2 // header('Content-Type: application/octet-stream'); // header("Content-Disposition: attachment; filename=$csvFileName"); // $file = new SplFileObject('php://output', 'w'); // foreach ($user as $row) { // $file->fputcsv($row); // } // $file->fflush(); // TODO: i want exclude debug infomation when output csv return view('home/index'); }
Why is debug code also output?
https://user-images.githubusercontent.co...94fb65.png
|