Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Help with csv export
  • Hello,
    The following code does not download unless I add a die statement.
    What am I doing wrong?
    Thanks

    [code]
     header('Content-Type: text/csv; charset=utf-8');
                header('Content-Disposition: attachment; filename=D1123813985.csv');
                $fp = fopen('php://output', 'w');

                $temp = array();
                foreach ($query as $row)
                {
                    $temp = array();
                    $temp[0] = '312858759';
                    $temp[1] = $row->name;
                    $temp[2] = $row->sku;
                    $temp[3] = '';
                    $temp[4] = '';
                    $temp[5] = '';
                    $temp[6] = '';
                    $temp[7] = '';
                    $temp[8] = '';
                    $temp[9] = '';
                    $temp[10] = '';
                    $temp[11] = '';
                    $temp[12] = '';
                    $temp[13] = $row->stock;
                    $temp[14] = '';
                    $temp[15] = '';
                    $temp[16] = '';
                    $temp[17] = '';

                    fputcsv($fp, $temp);
                }
                fclose($fp);
                die();
                $c = count($temp);
                //Response::redirect('admin/inventory/index');
    [/code]
  • HarroHarro
    Accepted Answer
    Not the best way to download something, you're using a framework so that will do the output processing for you, which interferes with your handcoded statements.

    Use File::download() instead. It requires a temp file you have to create (it has no support for inline data at the moment), but you can tell the method to delete it once it has been downloaded.

    This will makes sure the framework does a proper shutdown and cleanup before sending the file.
  • Thank you, I removed the header and wrote file to public. Then used FuelPHP File::download function.
  • HarroHarro
    Accepted Answer
    public?

    Why not create a temp file, it doesn't have to be publicly accessible at all. From a security point of view perhaps not a good idea either.
  • I will try doing that. Thanks Harro.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion