fopen/fput and includes

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
rfigley
Forum Commoner
Posts: 70
Joined: Sun Apr 21, 2002 7:10 pm

fopen/fput and includes

Post by rfigley »

I'm working on page to generte web pages using fopen: The page generated needs to have includes. So it would seem that I'm generating a page to include php scripting. In effect, nested php. Now berfore you say anything, I know thi is probably not the best way to do this but I have to get this working today and will refine it later. Here's the part of teh code I'm using.

Code: Select all

fputs($fp, "<HTML><HEAD>");
fputs($fp, "<TITLE>" . $chapter . "</TITLE>");
fputs($fp, "<META HTTP-EQUIV=Content-Type CONTENT=text/html; charset=iso-8859-1> ");
fputs($fp, "<link rel=stylesheet type=text/css href=npi.css>");
fputs($fp, "</HEAD>");
fputs($fp, "<BODY BGCOLOR=#FFFFFF topmargin=0 leftmargin=0>");...blah, blah..

/*HERE IS THE INCLUDE, I know the syntax is wrong but having difficulty with it. */

fputs($fp, include('incl_chapter_header.php'));

//Or something like this?

fputs($fp, "<? include('incl_chapter_header.php') ?>");
I'm getting a parse error with both versions I'm trying here.
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

you would need to escape the PHP tags, like so:

Code: Select all

fputs($fp, "\<\? include('incl_chapter_header.php') \?\>");
this prevents the parser from interpreting this code befroe it goes into the file.
seg
Forum Commoner
Posts: 38
Joined: Thu Oct 31, 2002 12:08 pm
Location: Northern, VA
Contact:

Post by seg »

mydimension wrote:you would need to escape the PHP tags, like so:

Code: Select all

fputs($fp, "\<\? include('incl_chapter_header.php') \?\>");
this prevents the parser from interpreting this code befroe it goes into the file.
That seems like the right solution, but if it is still not working, or if you have new problems elsewhere on the script it would help to post the error you get.

-seg
rfigley
Forum Commoner
Posts: 70
Joined: Sun Apr 21, 2002 7:10 pm

Post by rfigley »

INteresting, two guys saying the same thing. However I'm still getting the same error:

Parse error: parse error in /home/account/public_html/admin/chapter_page_generator3.php on line 48
seg
Forum Commoner
Posts: 38
Joined: Thu Oct 31, 2002 12:08 pm
Location: Northern, VA
Contact:

Post by seg »

rfigley wrote:Parse error: parse error in /home/account/public_html/admin/chapter_page_generator3.php on line 48

Code: Select all

&lt;?php
fputs($fp, "&lt;HTML&gt;&lt;HEAD&gt;"); 
fputs($fp, "&lt;TITLE&gt;" . $chapter . "&lt;/TITLE&gt;"); 
fputs($fp, "&lt;META HTTP-EQUIV=Content-Type CONTENT=text/html; charset=iso-8859-1&gt; "); 
fputs($fp, "&lt;link rel=stylesheet type=text/css href=npi.css&gt;"); 
fputs($fp, "&lt;/HEAD&gt;"); 
fputs($fp, "&lt;BODY BGCOLOR=#FFFFFF topmargin=0 leftmargin=0&gt;");...blah, blah.. 

/*HERE IS THE INCLUDE, I know the syntax is wrong but having difficulty with it. */ 

fputs($fp, include('incl_chapter_header.php')); 

//Or something like this? 

fputs($fp, "&lt;? include('incl_chapter_header.php') ?&gt;"); 
?&gt;
ok lets shorten this a bit

Code: Select all

&lt;?php
fputs($fp, "&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;$chapter&lt;/TITLE&gt;&lt;META HTTP-EQUIV=Content-Type CONTENT=text/html; charset=iso-8859-1&gt;&lt;link rel=stylesheet type=text/css href=npi.css&gt;&lt;/HEAD&gt;"); 

fputs($fp, "&lt;BODY BGCOLOR=#FFFFFF topmargin=0 leftmargin=0&gt;");...blah, blah.. 

fputs($fp, "&lt;? include('incl_chapter_header.php')?&gt;"); // is this line 48?
?&gt;
PHP should not have a problem with the <??> in the fputs function. It will, typically, blindly write whatever is in there until it gets to the next ". (this is my own personal expereicnce) Though hoestly I've never tried "<?..?>" specifically. If you could specify which line is line 48, then I'll try to work our a solution for that line.
seg
Forum Commoner
Posts: 38
Joined: Thu Oct 31, 2002 12:08 pm
Location: Northern, VA
Contact:

Post by seg »

just a guess. maybe if you went crazy with the escapes...

("\<\? include\(''incl_chapter_header.php''\)\;");
rfigley
Forum Commoner
Posts: 70
Joined: Sun Apr 21, 2002 7:10 pm

Post by rfigley »

OK, yes I agree.
I have two versions of this document and I may be showing the wrong coding. It would seem that this code is working, but not bringing back teh correct data, which of course has to do with the actual code of the include and not teh code calling teh include.

As you say the <? ?> shouldnot be a problem, and it' isn't, I've used it successfully, for this particular application in fact. Wasn't sure though about the syntax with it having to be generated in the page.

One thing that would help that i haven't found yet is doing a line break so the generated page is easier to read. Is it \n? . <BR> won't do it right because it's a line break for the html page being generated.
seg
Forum Commoner
Posts: 38
Joined: Thu Oct 31, 2002 12:08 pm
Location: Northern, VA
Contact:

Post by seg »

yeah \n is a line break
Post Reply