Author Topic: [SOLVED] MS Word download encoding error  (Read 1106 times)

0 Members and 1 Guest are viewing this topic.

Offline amillsmcewanTopic starter

  • Irregular
  • Posts: 9
    • View Profile
[SOLVED] MS Word download encoding error
« on: July 03, 2009, 11:44:48 AM »
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

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,581
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: MS Word download encoding error
« Reply #1 on: July 03, 2009, 11:56:38 AM »
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++)?
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline amillsmcewanTopic starter

  • Irregular
  • Posts: 9
    • View Profile
Re: MS Word download encoding error
« Reply #2 on: July 03, 2009, 01:10:37 PM »
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

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,581
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: MS Word download encoding error
« Reply #3 on: July 03, 2009, 01:14:00 PM »
You likely have two blank lines before the <?php tag in the file or between a closing ?> tag and the next opening <?php tag.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline amillsmcewanTopic starter

  • Irregular
  • Posts: 9
    • View Profile
Re: MS Word download encoding error
« Reply #4 on: July 03, 2009, 01:41:29 PM »
I'm sorry to be dim, but when you say "in the file", which file do you mean?

Offline amillsmcewanTopic starter

  • Irregular
  • Posts: 9
    • View Profile
Re: MS Word download encoding error
« Reply #5 on: July 03, 2009, 01:56:08 PM »
I have tracked down my problem thank you.  The variable $docarray["doctype_auto"]   is blank/null.  I shall now work on figuring out why!  Thanks again

Offline amillsmcewanTopic starter

  • Irregular
  • Posts: 9
    • View Profile
Re: MS Word download encoding error
« Reply #6 on: July 03, 2009, 02:16:37 PM »
oops.  Seems like I was premature.  The doctype field was blank in the database and yet this made no difference when downloading the file from the admin panel area of my site. 

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,581
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: MS Word download encoding error
« Reply #7 on: July 03, 2009, 02:20:00 PM »
I'm sorry to be dim, but when you say "in the file", which file do you mean?
The file that contains the code you posted, that is responsible for outputting the downloaded file.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline amillsmcewanTopic starter

  • Irregular
  • Posts: 9
    • View Profile
Re: MS Word download encoding error
« Reply #8 on: July 03, 2009, 02:52:01 PM »
I double checked and there are no spurious lines before the beginning or after the end.  I even deleted the php opening and closing lines and retyped them in case there were any hidden characters causing problems

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,581
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: MS Word download encoding error
« Reply #9 on: July 03, 2009, 03:08:07 PM »
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.)
« Last Edit: July 03, 2009, 03:10:17 PM by PFMaBiSmAd »
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline amillsmcewanTopic starter

  • Irregular
  • Posts: 9
    • View Profile
Re: MS Word download encoding error
« Reply #10 on: July 03, 2009, 05:30:22 PM »
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.