I'm using php to force the download of .bz2 archives and .deb files for a webservice I'm making, but after downloading the files are invalid. I opened both the original file and the file downloaded through php in a text editor and I noticed that t开发者_JS百科he one downloaded with php had 1 extra space at the beginning of the file, after removing that space the files did work. What could be causing this?
here is the code I'm using:
header('Content-Type: package/x-generic');
header('Content-Length: ' . filesize($file));
ob_clean();
ob_flush();
readfile($file);
exit;
I already tried experimenting with different headers and the clean and flush functions.
Any help or ideas will be greatly appreciated.
Probably because your PHP file(s) are saved as UTF-8 with a BOM (Byte Order Mark). Try saving your php files as UTF-8 without a BOM. Any reasonably advanced text-editor will allow you to save files as UTF-8 without a BOM.
Also see my answer to another question that is related to this. It also mentions what arnaud576875 talks about as being a possible cause of the problem.
You may have an extra space after a ?>
in some of you php files. It is generally a good idea to omit closing ?>
tags to avoid this problem.
If that's the case you could use headers_sent()
after flush()
to know from which file/line the extra space comes from.
If you have whitespace before a php tag that could cause the space at the beginning. If you need output buffering (ob_clean(), ob_flush()) chances are there is a space or data getting output somewhere prior to setting the headers. Turn off output buffering if possible and you should get an error telling what line the output started.
I finally found the solution to my problem, after a long search I found out that I was including another php file into my php file, that file had an space behind the ?> tag. :S Thanks to everyone who answered
精彩评论