With one of my projects I have users ‘export’ information into Excel files using PHPExcel. When it works, it works perfectly. Other times, only some people can actually open the files.
Since I need to store the exports that are created, they are saved on the server and then the created file is downloaded using readfile(). When it doesn’t work, (which is most of the time now) people get the ‘wrong file format’ error. The really odd thing is that since the file is saved, they can try downloading that same file again. Then it works.
Here is my final save and download code:
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$Temp_Export_File_Name.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
//$objWriter->save('php://output');
$objWriter->save('../project_files_images/created_exports/'.$Temp_Export_File_Name);
readfile('../project_files_images/created_exports/'.$Temp_Export_File_Name);
exit;
Is there something wrong with this code that I am missing? The problem tends to show itself most for users of the latest versions of Office (Mac or Windows)
Thanks for the input.
Mark’s tip was correct. For anyone else that might run into this, here is what was happening.
PHPExcel, for some reason, requires a ‘Creator’ and ‘Last Modified By’ field and I am not using them. I simply commented out the 2 lines of code within the PHPExcel code which referred to these 2 items.
It still doesn’t explain why it effected the forced file and not the saved file, but the theory I am running with is this.