Force download not working in mobile Safari

Topics: Developer Forum
Aug 22, 2012 at 2:45 AM
Edited Aug 22, 2012 at 3:07 AM

I'm generating Excel files on the fly within my PHP application. I'm using the Excel2007 format.

When a user visits the URL that creates and forces the download of the Excel file, everything works great in all browsers **except for mobile Safari** (iPhone and iPad).

Here are my headers and the `readfile` method:

header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-disposition: attachment; filename=' . $file_name . '.xlsx;');
header('Content-Length: ' . filesize($path_to_file . '.xlsx'));
readfile($path_to_file . '.xlsx');

When I browse in mobile Safari to the URL that is supposed to download the .xlsx file I can actually see the tabs representing each worksheet of the file, but I don't see the actual data like so:

Furthermore, there are two additional weird behaviors I'm encountering with this:

  • If I download this file on a desktop browser and email it to myself and open it with the Mail app in iOS, the file displays correctly.
  • f I then take that attachment from the Mail app in iOS and import into, say, Dropbox, it does NOT display properly (it displays the same as the screenshot above).
  • In Chrome, the file downloads properly and opens in Excel or even Numbers as expected, but in the console I see this message: Resource interpreted as Document but transferred with MIME type application/

Also, per the PHPExcel documentation, in place of readfile I have also tried:


That, however, produces an error in mobile Safari that reads: OfficeImportErrorDomain Error 912

To eliminate the Content-type as being the issue I've experimented by adjusting the `Content-type` to other values (such as application/ or even application/download). Unfortunately (though not surprisingly) those don't work either.

Any guidance is greatly appreciate.

By the way, I've posted this issue over at Stack Overflow as well.