0

I am using PHPPowerpoint to create a pptx file with some charts, and have no problem storing it in the same folder as the PHP script. PHPPowerpoint does that on itself.

I want to download the pptx file after it has been created, and so far I have tried every option I could locate on the web. This is how i try to do it atm.:

$file = str_replace('generate_report.php', 'export_download.pptx', __FILE__);
header('Content-Description: File Transfer');
header('Content-disposition: attachment; filename="' . $file . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation');
header('Expires: 0');
header('Cache-Control: ');
header('Pragma: ');
flush();
ob_clean();
readfile($file);

Nothing is downloaded when I execute the script. My pptx is created on the server and I can open it, no problem. But it wont download the file. I got the content type from this thread: What is a correct mime type for docx, pptx etc?. I also tried with many other types. When I console log my response i get a weird string (very long), beginning like this: PKCTDD����[Content_Types].xml͗�n�0E�|E�-J��*�X�����+���m���wBhE

Also tried this:

$handle = fopen($file, 'rb');
$buffer = '';
while (!feof($handle)) {
$buffer = fread($handle, 4096);
echo $buffer;
ob_flush();
flush();
}
fclose($handle);

Anybody who can help?

Community
  • 1
  • 1
Casper Slynge
  • 344
  • 2
  • 9
  • 19

1 Answers1

2

The following headers should work; and it's also better streaming directly to php://output rather than save to disk file and then spooling that disk file to the browser

// Redirect output to a client’s web browser
header('Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation');
header('Content-Disposition: attachment;filename="' . $file . '"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPPowerPoint_IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007');
$objWriter->save('php://output');
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Thanks for the reply. However, now its just stalling and i dont get any response. As soon as I remove the header('Content-Type... I suddently get a response again, but still nothing is downloaded. I dont need the readfile,flush,ob_clean ? – Casper Slynge Feb 05 '14 at 14:11
  • Check the headers that are actually being sent to the browser, to be certain you have nothing else that's triggering standard HTML headers from being sent – Mark Baker Feb 05 '14 at 14:14
  • Saving to `php://output` filestream should send the file directly to the current PHP output stream, which (in the case of the web SAPIs) should be directly to the browser – Mark Baker Feb 05 '14 at 14:15
  • Inside my .php script, there are no other headers tags. Is the code above, an example from your own project? Were you able to download the pptx file? – Casper Slynge Feb 05 '14 at 14:46
  • It seems like it can't accept the content-type. Its freezing as soon as I add that line. – Casper Slynge Feb 05 '14 at 14:55
  • A simple call to the `header()` function in a PHP script won't cause the script to freeze, no matter what arguments might be passed (even complete garbage in the header argument shouldn't cause a script to freeze) – Mark Baker Feb 05 '14 at 17:20
  • All I can see is that I get a response, as soon as i remove the content-type header line. But then it has to be an error in the file? Just cant understand why nothing is being downloaded. – Casper Slynge Feb 06 '14 at 12:24