Tuesday, March 26, 2013

Joomla! 2.5 / Apache: File Downloads, docx etc.

I let my users upload files and link to them in their articles. As I want to force them to download the files directly (and don't want them to view it in the browser), I am using a file called download.php which I include in the URLs.

Thus, instead of "/myfolder/myfiles/myfile.pdf" the href attribute of the link will be like "download.php?filename=/myfolder/myfiles/myfile.pdf".

In download.php I declare headers and do some things about the filename. It was always working fine for pdf or doc files, but there seemed to be something wrong with the "new" file formats of Office (docx, ppsx etc.) - if a user clicked on that file, it would be downloaded, but could not be opened (the message said something like "cannot read file"). A comparison of the original file and the downloaded file showed no difference except for one character which was not even visible. Fun fact: OpenOffice could open the file without any problems... Lots of pages suggested to check the MIME type configuration of apache in /etc/mime.types, but the docx formats were already there.

I did a lot of research and came across a post on http://php.net/manual/de/function.header.php by eonrglez at gmail dot com.

I included all the header information suggested in this post (except for the file size), from Pragma to Content-Transfer-Encoding, and voilĂ  - it works.

No comments:

Post a Comment