Jump to content

Recommended Posts

Hello Experts

 

There are two places on a website I am modifying where a user can download or open an MS word document.  On an Admin panel the administrator can click on a link to download the document; on another part of the website, a registered user can do the same.

 

The problem is this:  On the user part of the site, when MS word opens it comes up with an encoding error dialog box, asking the user to select the correct encoding so that the file can be opened in word.  This does not happen on the admin part of my site where the file opens correctly in word with no problems.

 

The very weird thing is that the PHP code used to download the file is IDENTICAL on both parts of the site.  I have even cut and pasted it from the working script to the one with an error and it makes no difference.  Does anyone have any ideas as to where else I could look for problem please - this is doing my head in !

 

Thank you

 

Link to comment
https://forums.phpfreaks.com/topic/164680-solved-ms-word-download-encoding-error/
Share on other sites

Either the code is not setting up a value or a header that the admin section is taking care of or a value it is using is different between the two sections. Cannot really help you without seeing the actual code and what have you done to check what is different between the downloaded files? Is the file size zero? Are the file sizes identical? Does it have extra characters at the beginning or end of the file (open it using a programming editor like notepad++)?

Thank you for your reply.  The "correct" download from the admin panel is size 69120 bytes (size on disk 69632).  The incorrect version is 69124 bytes (size on disk 69632).  If I use FTP to download the file it is identical to the "correct" version in content and size.

 

I downloaded notepad++ and the incorrect version has two blank lines at the front.  If I delete these in notepad++ and save back as a .doc file, it opens fine in word.  If the code is identical then I can only assume that one or more of the variables in the code must be incorrect.  Thank you for your help so far, I shall try and put some debug statmeents in the code to print out the variables.

 

The code is:

 

if($_GET["doc"])

{

$getdoc=mysql_query("select lname_auto,document_auto,doctype_auto from ".$this->prefix."candidate where id_auto='".$_GET["doc"]."' limit 1") 

or die($this->lang["17"]." ".mysql_error()."\n");

$docarray=mysql_fetch_array($getdoc);

 

$grabext=explode('.',$docarray["document_auto"]);

header("Content-Type: ".$docarray["doctype_auto"]."");

header('Content-Disposition: attachment; filename="'.$docarray["lname_auto"].$_GET["doc"].'.'.$grabext["1"].'"');

$gethtml=file_get_contents($this->path2doc.$docarray["document_auto"]);

echo $gethtml;

}

 

From what you've said it looks like I should see what the variables used in the "header" statements are.  Thank you

Is that file being included in another file?

 

Short answer - the content being output after the download link is clicked contains the extra blank lines. They are coming from somewhere or they are part of the actual file on the server and you are dealing with different files between the case where it works and the case where it does not.

 

Also, is the file saved as an ANSI (ASCII) file or is it saved as a UTF-8 encoded file (the BOM characters at the start of the file might be showing up as the extra blank lines.)

Fixed!  Thank you for the pointers about looking for blank lines.  I did manage to track down the source of two blank lines in a routine that preceded the one with the php code in it.  I will be on the lookout for these in the future if I get a similar error.  I'm grateful that you took the time to respond.  Thanks again.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.