Page 1 of 1

fopen/fput and includes

Posted: Fri Nov 08, 2002 10:36 am
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.

Posted: Fri Nov 08, 2002 10:38 am
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.

Posted: Fri Nov 08, 2002 10:40 am
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

Posted: Fri Nov 08, 2002 10:55 am
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

Posted: Fri Nov 08, 2002 11:16 am
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.

Posted: Fri Nov 08, 2002 11:18 am
by seg
just a guess. maybe if you went crazy with the escapes...

("\<\? include\(''incl_chapter_header.php''\)\;");

Posted: Fri Nov 08, 2002 11:39 am
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.

Posted: Fri Nov 08, 2002 12:11 pm
by seg
yeah \n is a line break