Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Controller/Component/CsvComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public function import($filename, $fields = array(), $options = array()) {
*/
public function export($filename, $data, $options = array()) {
$options = array_merge($this->defaults, $options);

if($filename === null){
$filename = "php://output";
$this->sendHttpHeaders();
}

// open the file
if ($file = @fopen($filename, 'w')) {
Expand Down Expand Up @@ -141,5 +146,26 @@ public function export($filename, $data, $options = array()) {
return false;
}
}

public function setFileNameOut($name = 'export-data.csv')
{
return $this->filename_out = $name;
}

public function getFileNameOut()
{
return $this->filename_out;
}

public function sendHttpHeaders()
{
header('Content-Encoding: UTF-8');
header('Content-Type: text/csv; charset=utf-8' );
header(sprintf( 'Content-Disposition: attachment; filename=%s', basename($this->getFileNameOut()) ) );
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of making export() more complex, why don't you add a download() method that calls export() internally and sets the headers and filename appropriately. This way you could take the download filename as an argument to download() and still pass php://output to the export() function.

}
?>