8

so this works:

myphpfile.php:

<?php
header('Content-disposition: attachment; filename=myfile.pdf');
header('Content-type: application/pdf');
readfile('myfile.pdf');
?> 

that php file is called here and the PDF download works fine:

<a class = "oglasavanje" href = "../../cjenik/myphpfile.php">download</a><br/>

but this doesn't work:

<?php
header('Content-disposition: attachment; filename=myfile.xlsx');
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
readfile('myfile.xlsx');
?>

both .pdf and .xlsx files are in the same folder. When I click on the 'download' link the download windows pops up, I save the file, but the file can't be opened. It gives me the following error:

Excel cannot open the file 'myfile.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

The file opens fine when I open it manually (i.e. without downloading it via the link)

I'm using WAMP, if that's of importance.

Any help is appreciated.

--- EDIT ---

I've found this piece of code on php.net:

ob_clean();
flush();

so the final code looks like this and it works:

$file = "myfile.xlsx";
header('Content-disposition: attachment; filename='.$file);
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
flush(); 
readfile($file);

Thanks everyone for the answers.

GTCrais
  • 2,039
  • 2
  • 26
  • 32
  • Do you calculate and send the size of the file? –  Apr 17 '12 at 20:24
  • Also, its helpful to know which browsers this fails in. IE has been known to have some issues downloading files of some types in some conditions. –  Apr 17 '12 at 20:26
  • I didn't calculate and send the size of the file (and I don't know how to do that). It's failing in Firefox. – GTCrais Apr 17 '12 at 20:37
  • you read the file and count the bytes. There's a guy below talking about some of the other headers that might help. –  Apr 17 '12 at 20:43
  • It would be great an exit; instruction just after readfile($file); – José Carlos PHP Jan 11 '21 at 12:19

4 Answers4

7

Depends on your browsers so many things can cause such behavior such as Content-length , Cache-Control etc

Also Change

Content-type to Content-Type

Content-disposition to Content-Disposition

Try

$file = "myfile.xlsx" ;
header('Content-Disposition: attachment; filename=' . $file );
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile('myfile.xlsx');
Baba
  • 94,024
  • 28
  • 166
  • 217
  • Explain .. what do you mean it did not work ? what exactly is the error you are getting .... just tested it it works here – Baba Apr 17 '12 at 20:51
  • The error is explained in the original post. In the meantime I've figured it out. – GTCrais Apr 17 '12 at 20:54
2

Depending on your web browser, the filename may not be saving fully with periods. If the filename is incomplete, you can try and replace this header:

header('Content-disposition: attachment; filename=myfile.xlsx');

with

$filename = "myfile.xlsx";
header('Content-Disposition: attachment; filename="' . $filename . '"');
Scryptronic
  • 111
  • 1
  • 4
1
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($filename);
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($filename));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile($filename);
return;

This solution works for me, but when i'm trying to open downloaded file i'm getting an error. For .pdf file is just empty.

0
$file = "abc.xls";

header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
helvete
  • 2,455
  • 13
  • 33
  • 37