setFileName($fileName); } } /** * * set file name * * @param string $fileName * * @return $this */ public function setFileName($fileName) { $this->_fileName = $fileName; return $this; } /** * * get file name * * @return string */ public function getFileName() { return $this->_fileName; } /** * * get heading array * * @return array */ public function getHeading() { return $this->_heading; } /** * * set heading array * * @param array $heading * * @return $this */ public function setHeading($heading) { $this->_heading = $heading; return $this; } /** * * get data * * @return array */ public function getData() { return $this->_data; } /** * * set data * * @param array $data * * @return $this */ public function setData($data) { $this->_data = $data; return $this; } /** * * generate csv file from array and send it to download * * @return void */ public function send() { if (empty($this->_fileName)) { throw new \InvalidArgumentException("A file name must be set."); } $this->setHeader('Content-Type: text/csv') ->addHeader('Content-Disposition: attachment; filename="' . $this->_fileName . '"') ->addHeader('Pragma: no-cache') ->addHeader('Expires: 0'); $output = fopen("php://output", "w"); if (!empty($this->_heading)) { fputcsv($output, $this->_heading); } foreach ($this->_data as $row) { fputcsv($output, $row); } fclose($output); parent::send(); exit(0); } }