Help with PHP and MSWORD docs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lsnyder
Forum Newbie
Posts: 4
Joined: Sat May 13, 2006 11:14 pm
Location: York, PA

Help with PHP and MSWORD docs

Post by lsnyder »

I wrote a script that replaces "template vars" in a word document with values a user submits from a form. When the submit button is hit, the template MSWORD document is opened up and read into variable. The "template vars" are then replaced with the form values and the document is saved and the user may then download the document. But when you attempt to open up the document, you get an error that says :


"Word experienced an error trying to open this file. Try these suggestions. *Check the file permissions for the document or drive *make sure there is sufficient free memory and diskspace * open the file with the text recovery converter"

When I comment out the loop that replaces the "template vars" so no changes to the template file are made, and php saves the file, you can open it and view it regularly, so i know the problem is something with the str_replace, preg_replace, ereg_replace functions i've tried. All get the same error when they're used and you try to open the documents.

What do i need to do so that the word document is viewable?

Someone please help! If you want to view the files, you can download them in zip format here:

http://www.lancesnyder.com/problem.zip

If someone could please help me i would REALLY appreciate it!!
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

what happens when you echo the 'modified' string before 'injecting' it into word?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Make sure the functions you are using to make the replacements are 'binary-safe' if they are not word will believe the file to be corrupt.

Of course Word may well create checksums etc that prevent you from doing such a thing.
lsnyder
Forum Newbie
Posts: 4
Joined: Sat May 13, 2006 11:14 pm
Location: York, PA

Post by lsnyder »

When I echo the 'modified' string, it prints the 'special characters' word uses and the text from the document. I can see that the string contains the values from the form, when it's saved though it's not able to be opened.

What functions would be binary safe?
lsnyder
Forum Newbie
Posts: 4
Joined: Sat May 13, 2006 11:14 pm
Location: York, PA

Post by lsnyder »

Here is the part of the script that replaces the values in the form

Code: Select all

$template_vars = array(
					  "{LOCATION_NAME}" => $values['location_name'],
					  "{LOCATION_ADDR}" => $values['address'],
					  "{CITY_STATE_ZIP}" => $values['city'].", ".$values['state']." ".$values['zipcode'],
					  "{PHONE}" => $values['phone_areacode']."-".$values['phone1']."-".$values['phone2'],
					  "{FAX}" => $values['fax_areacode']."-".$values['fax1']."-".$values['fax2'],
					  "{NUM_ROOMS}" => $values['number_rooms'],
					  "{PRICE_PER_ROOM}" => $values['price_room'],
					  "{QUARTERLY_PAYMENT}" => $values['quarterly_payment']);

	//open template
	$location = "/var/www/vhosts/myactivesupport.com/htdocs/service_contracts/support_agreement_template.doc";
	$file = fopen($location,"rb");
	$data = fread($file,filesize($location));
	fclose($file);

	//replace template vars with values
	foreach ($template_vars as $template_var => $value) {
		$data = str_replace($template_var, $value, $data);
	}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

str_replace() is binary safe, so it probably is not that. You could test if is a checksum problem by replacing your tags with strings of the same length (e.g. "{LOCATION_NAME}" => '123456789012345' ) and see if you still have the problem.
(#10850)
lsnyder
Forum Newbie
Posts: 4
Joined: Sat May 13, 2006 11:14 pm
Location: York, PA

Post by lsnyder »

I solved the problem by converting the documents to RTF. Thanks everyone for your effort in trying to help me solve this problem though!!
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I solved the problem by converting the documents to RTF. Thanks everyone for your effort in trying to help me solve this problem though!!
Good idea
What functions would be binary safe?
The manual will always tell you
Post Reply