Solved

Header download of word doc is jumbled

Posted on 2008-06-11
2
2,455 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now