Skip to content

Instantly share code, notes, and snippets.

@mathewbyrne
Created March 21, 2013 22:54
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save mathewbyrne/5217561 to your computer and use it in GitHub Desktop.
Save mathewbyrne/5217561 to your computer and use it in GitHub Desktop.
A small Symfony 2 class for returning a response as a CSV file. Based on the Symfony JsonResponse class.
<?php
namespace Jb\AdminBundle\Http;
use Symfony\Component\HttpFoundation\Response;
class CsvResponse extends Response
{
protected $data;
protected $filename = 'export.csv';
public function __construct($data = array(), $status = 200, $headers = array())
{
parent::__construct('', $status, $headers);
$this->setData($data);
}
public function setData(array $data)
{
$output = fopen('php://temp', 'r+');
foreach ($data as $row) {
fputcsv($output, $row);
}
rewind($output);
$this->data = '';
while ($line = fgets($output)) {
$this->data .= $line;
}
$this->data .= fgets($output);
return $this->update();
}
public function getFilename()
{
return $this->filename;
}
public function setFilename($filename)
{
$this->filename = $filename;
return $this->update();
}
protected function update()
{
$this->headers->set('Content-Disposition', sprintf('attachment; filename="%s"', $this->filename));
if (!$this->headers->has('Content-Type')) {
$this->headers->set('Content-Type', 'text/csv');
}
return $this->setContent($this->data);
}
}
@thomasbrix
Copy link

thomasbrix commented Dec 1, 2018

Thanks.
@glauberm there's a small misprint in

foreach ($data as $row) { 
    fputcsv($output, $row); 
}

should be

foreach ($data as $row) { 
    fputcsv($handle, $row); 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment