Solved

Header download of word doc is jumbled

Posted on 2008-06-11
2
2,461 Views
Last Modified: 2008-06-11
Hello,

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) . '"');
 
flush();
 
$file = fopen($fullpath, 'r');
while (!feof($file))
{
	print fread($file, filesize($fullpath));
	
	flush();
}
fclose($file);

Open in new window

0
Comment
Question by:snakeriver
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
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.

<?PHP
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;
fclose($handle);
flush();
?>

Open in new window

0
 

Author Comment

by:snakeriver
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:

<?php
code here...
?>

<?php
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...
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

623 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