amillsmcewan Posted July 3, 2009 Share Posted July 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/164680-solved-ms-word-download-encoding-error/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 3, 2009 Share Posted July 3, 2009 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++)? Quote Link to comment https://forums.phpfreaks.com/topic/164680-solved-ms-word-download-encoding-error/#findComment-868443 Share on other sites More sharing options...
amillsmcewan Posted July 3, 2009 Author Share Posted July 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/164680-solved-ms-word-download-encoding-error/#findComment-868462 Share on other sites More sharing options...
PFMaBiSmAd Posted July 3, 2009 Share Posted July 3, 2009 You likely have two blank lines before the <?php tag in the file or between a closing ?> tag and the next opening <?php tag. Quote Link to comment https://forums.phpfreaks.com/topic/164680-solved-ms-word-download-encoding-error/#findComment-868464 Share on other sites More sharing options...
amillsmcewan Posted July 3, 2009 Author Share Posted July 3, 2009 I'm sorry to be dim, but when you say "in the file", which file do you mean? Quote Link to comment https://forums.phpfreaks.com/topic/164680-solved-ms-word-download-encoding-error/#findComment-868481 Share on other sites More sharing options...
amillsmcewan Posted July 3, 2009 Author Share Posted July 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/164680-solved-ms-word-download-encoding-error/#findComment-868487 Share on other sites More sharing options...
amillsmcewan Posted July 3, 2009 Author Share Posted July 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/164680-solved-ms-word-download-encoding-error/#findComment-868499 Share on other sites More sharing options...
PFMaBiSmAd Posted July 3, 2009 Share Posted July 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/164680-solved-ms-word-download-encoding-error/#findComment-868501 Share on other sites More sharing options...
amillsmcewan Posted July 3, 2009 Author Share Posted July 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/164680-solved-ms-word-download-encoding-error/#findComment-868516 Share on other sites More sharing options...
PFMaBiSmAd Posted July 3, 2009 Share Posted July 3, 2009 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.) Quote Link to comment https://forums.phpfreaks.com/topic/164680-solved-ms-word-download-encoding-error/#findComment-868525 Share on other sites More sharing options...
amillsmcewan Posted July 3, 2009 Author Share Posted July 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/164680-solved-ms-word-download-encoding-error/#findComment-868560 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.