Header download of word doc is jumbled

Posted on 2008-06-11
Last Modified: 2008-06-11

I'm trying to send a file to a browser.  The file type is a word doc.  When the word doc downloads, and I try to open it, the text is all jumbled.  This code works fine for pdf's and images by changing the Content-Type accordingly...
if(!is_file($fullpath)) {
  exit('File not found');
header('Cache-control: private');
header('Pragma: public');
header('Content-Type: application/msword');
header('Content-Length: ' . filesize($fullpath));
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename="' . urldecode($path) . '"');
$file = fopen($fullpath, 'r');
while (!feof($file))
	print fread($file, filesize($fullpath));

Open in new window

Question by:snakeriver
LVL 29

Accepted Solution

rdivilbiss earned 500 total points
ID: 21764913
This simpler version works for me, even with very large documents.

Not sure you want the fush after the headers, but maybe no harm.

if(!is_file($fullpath)) {
  exit('File not found');
header('Content-Type: APPLICATION\MSWORD');
header('Content-Length: ' . filesize($fullpath));
header('Content-Disposition: attachment; filename="' . urldecode($fullpath) . '"');
$handle = fopen($fullpath, "rb");
$contents = fread($handle, filesize($fullpath));
print $contents;

Open in new window


Author Comment

ID: 21764934
THanks rdivilbiss, I found what the problem was.  I was including a file at the top of my page, and in that include file, there were two php statements seperated by a space:

code here...

code here...

I got rid of the space between the two, and it works just fine.  I guess it was just putting an extra line break before the header output...

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Email called spam 5 36
Custom Wordpress Loop 22 38
Php logic to add to date card 9 22
How would I craft a PDO SELECT based on an IF Statement? 8 6
This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question