Downloading from private directory corrupting some file types We have set up a private file system on a website based on drupal's guidelines. We are using Drupal 8.

We have setup the private file directory outsite of /var/www at /var/privatefiles. When downloading certain types of files - specifically docx - files are being corrupted. pdfs and other common types seem to be working fine.

File upload is working correctly as I am able to scp the files directly from the server and they are intact.

Additionally, on a test server made from a copy of the server we set up the private files inside the siteroot and that works without problem. This solution is not ideal however as we do not want the files to be accessible directly from the web. 

I cannot find anything that discusses this issue, surely I am not the first person to set up a private directory this way and have docx files break?

UPDATE:

Further testing indicates that what is occurring is that drupal or php are adding 3 newline 0a characters to the start of every file served from the private file system. It has np effect on some formats but all MS files - docx,xlsx etc - are broken.

How do I fix this?
We have finally solved the issue and in case it is of use will put the answer here.

Based on this php issue we realised the issue could be a closing php tag somewhere else on the site. 

In the end we found three small modules - one animated a set of banner images on the site; one made some api calls for a search feature; and another created some custom blocks from an external api call. 

Each of these had ?&gt; at the end of their .module files. Removing them fixed the problem.

Why this problem went on to affect all files served I do not know.