I have an e-commerce website and I am trying to have a webpage create a .doc or .rtf document to print labels based on the customers' addresses in a mysql database.

For this reason, I need to create a document based on a labels template.

I have saved the labels template as whslabels.rtf on the website, and on every label I have written "Address1", "Address2", "Address3", ... then I have the mentioned webpage do a str_replace replacing the "Address..." words with the customer's address, and then save this to a new file named "newlabels6.rtf".

$template_file = "whslabels.rtf";
$handle = fopen($template_file , "r");
$contents = fread($handle, filesize($template_file));

$original= array("Address1", "Address2");
$new = array("pizza", "beer");

$newphrase = str_replace($original, $new, $contents);

$handle2 = fopen("newlabel6.rtf" , "w");

fwrite ( $handle2 ,$newcontents);
fclose ($handle);
fclose($handle2);

but when I go to open the new .rtf file it doesn't seem to able to open it. The exact error I get is "The document file or path is not valid...."

What am I doing wrong?

Recommended Answers

All 2 Replies

Are you writing plain text to a rich text file? That may not be the source of your problem, but it could be the source of another problem later.

I'd double check my paths and capitolzation, my quick look at your code segment didn't pick up anything immediately wrong.

One more thing, you only need to use either [ code ] or [ php ] tags, not both. The reason it failed to display properly is because you didn't turn them off (using [ /code ] or [ /php ]). BBCode like this takes a bit to get used to.

$template_file = "whslabels.rtf";
$handle = fopen($template_file , "r");
$contents = fread($handle, filesize($template_file));

$original= array("Address1", "Address2");
$new = array("Here is address one", "Here is address 2");

$newphrase = str_replace($original, $new, $contents);

$handle2 = fopen("newlabel6.rtf" , "w");

fwrite ( $handle2 ,$newcontents);
fclose ($handle);
fclose($handle2);

Basically the template is a typical sheet of 21 labels made with Word, and originally saved as a .dot file. The addresses are made up in HTML by the php page, but instead of displaying the on the screen is trying to subsitute address 1 with lets say:

Mr John Smith<BR>
Flat 12, 42 Piccdilly Circus<BR>
W1<BR>
London<BR>
UK

I have never done this before, and that is the only thing I could think of to reach my objective: having a page of lables ready to print with the address of the day's customers' orders.

The .dot file has originally Address1, Address2, Address3, ... where the addresses will go.

but when I go to open the new .rtf file it doesn't seem to able to open it. The exact error I get is "The document file or path is not valid...."

What am I doing wrong?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.