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!!
Help with PHP and MSWORD docs
Moderator: General Moderators
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
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);
}- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK