1

I have the following lines of code:

$objPHPExcel = new PHPExcel();

..ADDITIONAL CODE..

// Redirect output to a client.s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="powers-id_report.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');

When this code is executed from a PHP file (outside of a class) the Excel file is created and saved to the browser's download directory.

I then moved all of the code to it's own class:

class ReportController extends BaseController {
   function generate() {
      $objPHPExcel = new PHPExcel();

      ..ADDITIONAL CODE..

      // Redirect output to a client.s web browser (Excel5)
      header('Content-Type: application/vnd.ms-excel');
      header('Content-Disposition: attachment;filename="powers-id_report.xls"');
      header('Cache-Control: max-age=0');
      $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
      $objWriter->save('php://output');

      $this->fw->reroute('/');
   }
}

And it fails. I am using Fat Free PHP to redirect the request to this class. I've used break points to see that it is still executing all of the code the same as before but no file is being outputted. The $objWriter->save('php://output'); is returning true which should mean it successfully saved the file. I have also tried removing the reroute. My error logs are all empty as well.

Jeremy
  • 3,620
  • 9
  • 43
  • 75
  • If your error logs are empty, could it be because you have set PHP not to report any errors – RiggsFolly May 18 '16 at 15:41
  • Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly May 18 '16 at 15:42
  • Also where is `$objWriter` instantiated. Is it in scope now you move the code into a class method?? – RiggsFolly May 18 '16 at 15:43
  • I am already outputting errors but added the lines anyway just in case with no luck. I do not believe there is an error because everything else works, just no file is outputted. Also, I added the $objWrite instantiation to my code above. – Jeremy May 18 '16 at 15:46
  • Not familiar with FatFree.... Does `$this->fw->reroute('/');` redirect to `/`? If so, try die()ing instead rather than letting FatFree send its own headers to redirect, which is probably discarding the PHPExcel output – Mark Baker May 18 '16 at 18:26
  • It does reroute, but I even tried removing that line with no luck. It does seem that the problem is with F3 trying to reroute the output. I am just not sure how to get it to stop. @MarkBaker – Jeremy May 18 '16 at 18:29
  • 1
    Did you try `die()` after the save? Though I'm pretty sure that F3 will have some mechanism that allows you to set headers in your own controllers and prevent F3 from overriding them; most frameworks do – Mark Baker May 18 '16 at 18:34
  • That did the trick! I removed the redirect and replaced it with die. If you submit an answer I will accept it. Thank you much! @MarkBaker – Jeremy May 18 '16 at 18:42

1 Answers1

1

The solution is

$objWriter->save('php://output');
die();

This prevents execution of $f3's controller functions such as afterroute which may change headers.

Njuguna Mureithi
  • 3,506
  • 1
  • 21
  • 41