By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

How does this piece of code work?

P: 10
Hi Guys,

I wrote a piece of code by piecing together bits from tutorials on fopen and outputting it in word doc.

But looking back I don't get how it works. Here is the code:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.     <title>Some title</title>
  4. </head>
  5.  
  6. <body>
  7. <h1>A title </h1>
  8.  
  9. <?php 
  10.         $content="<html><p><h2>some text</h2></p></html>";
  11.         print $content;
  12.  
  13.         $myFile = "my_report.doc";
  14.         $fh=fopen($myFile, 'w') or die("cant write file");
  15.  
  16.         fwrite($fh, $content);
  17.  
  18.         fclose($fh);
  19.  
  20.  
  21.         // Set up the file name
  22.         $fileName = "my_file";
  23.  
  24.         header("Content-type: application/vnd.ms-word");
  25.         header('Content-Disposition: attachment; filename="'.$fileName.'.doc"');
  26.  
  27.  
  28. ?>
  29. </body>
  30. </html>
  31.  
There are 2 things I fail to understand.

1) The $fileName onwards has no link to the code involving fopen to fclose, so not sure how it reads the file I have written to.

2) I want it to only write whatever is written in $content. However this piece of code writes both:

A Title

Some text

Would be great if someone could show me how this works. Many thanks.
Feb 9 '12 #1
Share this Question
Share on Google+
3 Replies


P: 14
it writes "a title" because it is in an h1 tag in the body of the html file
as for the some text it is in a h2 tag and i dont know how to help you on that one.
Feb 9 '12 #2

P: 10
Thanks Phillis,

I fiddled around a bit more. The fopen bit is irrelevant. I can remove it will still work fine.

I think its got to do with the command print

The code is:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.     <title>Some title</title>
  4. </head>
  5.  
  6. <body>
  7. <h1>A title </h1>
  8.  
  9. <?php 
  10.         $content="<html><p><h2>some text</h2></p></html>";
  11.         //print $content;
  12.  
  13.         // Set up the file name
  14.         $fileName = "my_file";
  15.  
  16.         header("Content-type: application/vnd.ms-word");
  17.         header('Content-Disposition: attachment; filename="'.$fileName.'.doc"');
  18.  
  19.  
  20. ?>
  21. </body>
  22. </html>
  23.  
  24.  
it Yeilds A Title


If i uncomment the print, it then yeilds:

Some Title

A Title
Feb 9 '12 #3

dlite922
Expert 100+
P: 1,584
Lesson to learn: Don't modify header AFTER you've already sent content (in your case you've sent the all the characters "<html> to ...title</h1" and then you modify it.

$fileName is just a variable that contains the name of the attachment, whatever you want to call it. If you change that line to

Expand|Select|Wrap|Line Numbers
  1.         header('Content-Disposition: attachment; filename="chicken.doc"');
The file will be called chicken.doc. The CONTENTS will be whatever is printed so far. (If modifying headers after sending it, even works, I'm pretty sure it doesn't)

If you want to write some content then 'download' it. create a form that submits the fields to a different php page. Capture the output into $content, modify the headers(), THEN call print $content.

Good luck,


Dan
Feb 10 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.