0

I trying to force a download by the browser here is my code:

header("Content-Type: application/force-download"); 
header('Content-type: audio/mp3');
header('Content-Description: File Download');
header('Content-Disposition: attachment; filename=' . $file_name);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-length: ' . filesize($file_path);
ob_clean();
flush();
readfile($file_path);

This works perfectly on my local machine, but when I upload it to my live server, the binary file is dumped to the browser and those gibberish characters fill up the browser window. What could be the problem? I am on a shared server so I don't know if my apache configuration needs to be changed or something.

I took @sanmi advice and used Firebug to see the response header and here is what I got:

Here is what I got sanmai: 
Server  nginx/0.7.67
Date    Tue, 06 Sep 2011 15:02:03 GMT
Content-Type    text/html; charset=UTF-8
Transfer-Encoding   chunked
Connection  keep-alive
Expires Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0   
Pragma  no-cache
Vary    Accept-Encoding,User-Agent
Content-Encoding    gzip

I can see that the content-type entry has changed to text/html, and that the server is actually a nginx one. So is this a nginx issue or are my header entries wrong?

Thanks, Tsega

2
  • If you comment ob_clean() and flush() in the server, what's the reponse? Commented Sep 6, 2011 at 9:37
  • done that but still same problem. Commented Sep 6, 2011 at 14:59

4 Answers 4

1

It turns our one of the files I include had a blank line after the closing php tag. That was the problem, thanks everyone for your help.

Sign up to request clarification or add additional context in comments.

1 Comment

thx a lot. I had an include file that had ended like this: ... readfile($file); exit; } } ?> </div> ----------------------- now ends like this: readfile($file); exit; } } ?> and works perfectly. No more binary symbols in the webpage! (a TONS OF THANKS !!!)
0

Here

header("Content-Type: application/force-download"); 
header('Content-type: audio/mp3');

You send two Content-Type, only one is necessary.

5 Comments

It doesn't matter. The second will overwrite the first.
Because he is trying to force the download, the override matter.
It may not override at all because "This function now prevents more than one header to be sent at once as a protection against header injection attacks." php.net/manual/en/function.header.php
I understand this sentence like it's not more possible to do something like this header("Header-Y: Hello\nHEader-X: World");
removed the one with audio/mp3 and tried still didn't work. It still works locally though.
0

I am not sure if this might be causing it, but content type audio/mp3 isn't defined(officially): http://www.iana.org/assignments/media-types/audio/index.html . Try using audio/mpeg?

1 Comment

I tried you suggestion but the problem still persists. I'm guessing it has to do with the server.
0

Use FireBug or other means to view HTTP headers your server actually sending. It may hide or alter any of them. If so, talk to your hosting's support.

3 Comments

Add this to your original question, please. Seems to me that you hosting server eats all those headers. Check with the support, we can't help you here.
I used FireBug and I see that the response header's Content-Type: has changed to text/html; charset=UTF-8. That shouldn't be the case right, I see that the server is a nginx server not an apache one, could that be the issue?
Thanks @sanmai, I was really suspecting that, I read something on their Knowledge Base, that they use load balancing and have special header info or something like that so I'll check with them. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.