Unexpected newline prepended to file

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Hi all,

This is doing my head in!

I've got what should be a very simple script to download a file.
Actually it does lots of other things - but I can replicate the
behaviour easily using this:

               header("Cache-Control: max-age=30, must-revalidate");
                header("Content-Disposition: attachment; filename=
                header("Content-Type: application/msword");
                print file_get_contents('/home/colinm/msword.doc');

However, MS Word reports that it is not a doc file and says I need to
install a filter. The original file IS a msword document, but the file
is somehow getting prepended with a newline character on its journey.

All the headers are getting sent correctly:

GET /opstatsgraph/test.php HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, application/vnd.ms-excel, application/
vnd.ms-powerpoint, application/msword, application/x-silverlight,
application/x-silverlight-2-b2, */*
Accept-Language: en-gb
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;
SV1; .NET CLR 2.0.50727)
Host: dev-sensor.slc.co.uk
Connection: Keep-Alive

HTTP/1.1 200 OK
Date: Tue, 24 Nov 2009 16:24:49 GMT
Server: Apache/2.2.3 (Red Hat)
X-Powered-By: PHP/5.1.6
Cache-Control: max-age=30, must-revalidate
Content-Disposition: attachment; filename="test.doc"
Content-Length: 0
Connection: close
Content-Type: application/msword

Can anyone see what I'm doing wrong?

Oddly if I just do this:

 header('Content-Type: application/msword');
 print file_get_contents('/home/colinm/msword.doc');

Then the MSWord file opens as expected, however I need to force the
download box to rpevent MSIE from viewing the file in the browser



Re: Unexpected newline prepended to file

Quoted text here. Click to load it

An ascii newline character (LF/0x0A)

Quoted text here. Click to load it

Vim on Linux

Quoted text here. Click to load it

How would this cause a newline character to be output before the file?
Certainly if it appeared earlier in the file it could be a problem -
but that would also prevent the headers being sent - which is not the

BTW - cut and paste error in my post above - the response headers
should be:

HTTP/1.1 200 OK
Date: Wed, 25 Nov 2009 10:05:15 GMT
Server: Apache/2.2.3 (Red Hat)
X-Powered-By: PHP/5.1.6
Content-Disposition: attachment; filename=3D"test.doc"
Cache-Control: max-age=3D30, must-revalidate
Connection: close
Transfer-Encoding: chunked
Content-Type: application/msword

Note that even if I use ob_start() it still sends it as Transfer-
Encoding: chunked


Site Timeline