473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How does this piece of code work?

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
3 1391
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
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
1,584 Expert 1GB
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

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

Similar topics

2
by: blongmire | last post by:
.... I know just enough to be dangerous, but the real danger is that I might fall asleep and hit my head on my keyboard waiting for this code to finish executing. Some preliminaries: WinXP Pro,...
3
by: xmail123 | last post by:
Why does this code work? I am new to C# and have been studying this piece of code. It loops through an Adjacency Matrix table to populate a tree view. I have two questions about why this code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.