Reputation: 1396
There are variants of this question. Unfortunately they couldn't help me.
The below code works perfectly fine (using slim). User header() if not using slim.
$response = $res->withHeader('Content-Description', 'My File transfer')
->withHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
->withHeader('Content-Disposition', 'attachment;filename="'.basename($filePath).'"')
->withHeader('Expires', '0')
->withHeader('Content-Transfer-Encoding', 'binary')
->withHeader('Cache-Control', 'must-revalidate')
->withHeader('Pragma', 'public')
->withHeader('Content-Length', filesize($filePath));
readfile($filePath);
I tried ob_start, flush, end combination from the other answers but nothing helped.
I was able to open the file on the server but as soon as I tried to open it after download through above code Excel could not open it - Excel cannot open the file because the file format or file extension is not valid.
Upvotes: 0
Views: 802
Reputation: 1396
Open the files (source and downloaded) in hex to look for any obvious difference.
In my case it was a leading 0d0a (line feed and carriage return)
This was coming from a new line after ?> (php close tag) !!! I removed the new line after ?> and I could get it working (could open the file)
I hope it helps someone. Just lost 3 hours figuring out.
Upvotes: 1