Welcome Guest, Not a member yet? Register   Sign In
i want exclude debug infomation when output csv
#1

hello Smile

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
Reply
#2

You may need to turn off the Debug Toolbar.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Use $option debug. See https://codeigniter4.github.io/CodeIgnit....html#view

PHP Code:
view('view_file'$data, ['debug' => false]); 
Reply
#4

(This post was last modified: 07-23-2022, 04:40 PM by yoshi.)

@InsiteFX  @kenjis
thanks every time. Smile
The cause this time seemed to be that a string was returned upon return.
I stopped returning view and replaced it with exit

PHP Code:
    public function csvExport(): string
    
{
          :
        // close stream
        fclose($stream);
        
        
// TODO: Debugging information is attached to the output CSV.
        // return view('home/index');
        exit();
    

Why can I write to a closed CSV? after fclose command.
Is 'php://output' involved?
Reply
#5

In fact, CI4 does not support stream output except in case of downloading an existing file.
See https://codeigniter4.github.io/CodeIgnit...e-download

Even if you want to output stream in your controller, all output will be buffered and sent by the framework.

If you need to disable the output buffering, put the following code before your stream output.

PHP Code:
while (ob_get_level() > 0) {
    ob_end_clean();

Reply
#6

@kenjis
thanks. I understanded this fact.
I was referring to was probably not the CodeIgniter4 way.

I have searched for "Codeigniter4 csv" and found that the way to do this `exit()` seems to be ok for now.
https://makitweb.com/how-to-export-data-...igniter-4/
Reply
#7

The code in that article is terrible, but it is also true that there is currently no way to write it cleanly.

I suppose it would be better to add a StreamResponse class to be able to return a stream.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB