Ask a Question related to PHP Development, Design and Development.
-
Vince Lamonica #1
using fwrite to create PHP files
Hi all,
I wish to use fwrite() to create a small PHP file. So far, when I attempt
to do this, php parses the contents of the file that fwrite needs to
create. Eg, I have this:
$new_id = mysql_insert_id();
// create brand new file
$new_file = fopen("/var/www/html/$sitename/$submitted_url", "w");
$new_file_content = "\n"
.." <?php \n"
.." if ($_GET['preview'] == \"y\") { \n"
.." readfile(\"http://www.foo.bar/${'sitename'}.display?${'sitename'}id=$new_id&prev iew=y\"); \n"
.." } else { \n"
.." readfile(\"http://www.foo.bar/${'sitename'}.display?${'sitename'}id=$new_id\"); \n"
.." } \n"
.." ?>";
write($new_file, $new_file_content);
fclose($new_file);
Note that $sitename is defined earlier in the script.
I know that $new_id and $sitename get expanded, as I tried this:
$new_file_content = "hello world. Look at my ${'sitename'}.display and my $new_id";
and the new file was created with the above text and the two vars
expanded. My problem lies in the fact that I need to put "<?php" tags
inside the contents of $new_file_content. I'm not sure how to do that w/o
generating lots of errors. As you can see in my above example, I've tried
to escape the double quotes, but that didn't seem to help. PHP insists on
processing the "if" line instead of ignoring it.
I'm running 4.3.3 under apache 1.3.28.
Any help would be most appreciated!
/vjl/
--
Vince LaMonica UC Irvine, School of Social Ecology
W3 Developer <*> 116 Social Ecology I, Irvine, CA 92697
[email]vjl@uci.edu[/email] [url]http://www.seweb.uci.edu/techsupport[/url]
Tower: "Delta Zulu Romeo, turn right now and report your heading."
Pilot: "Wilco. 341, 342, 343, 344, 345..."
Vince Lamonica Guest
-
Create Adobe PDF files from PDF files?
Is is possible to create PDF files from an existing PDF file? For example, I have a PDF file that contains 2 pages of content. ("pages1and2.pdf")... -
[PHP] using fwrite to create PHP files
I wish to use fwrite() to create a small PHP file. So far, when I attempt to do this, php parses the contents of the file that fwrite needs to... -
Is fwrite atomic or not?
I've seen answers both ways in the archive of this list, and I wonder what is true. Suppose I open a file in append mode - like a log file. Two... -
fwrite int or float
Is there some easy way to fwrite a 32 bit int or float? -- Ted Huntington Programmer Analyst I Main Library University of California, Irvine... -
How to create help files / text files
I want to create a help dialogue that will just act as a reminder on do's & dont's I had in mind a text file with sliding bars but don't seem to... -
Bogdan Stancescu #2
Re: using fwrite to create PHP files
To answer your question, you probably can do
$new_file_content="<"."?"."php\n" . [...] . "?".">";
But why in the world do you need this complication?
Bogdan
Vince Lamonica wrote:
> Hi all,
>
> I wish to use fwrite() to create a small PHP file. So far, when I attempt
> to do this, php parses the contents of the file that fwrite needs to
> create. Eg, I have this:
>
> $new_id = mysql_insert_id();
> // create brand new file
> $new_file = fopen("/var/www/html/$sitename/$submitted_url", "w");
> $new_file_content = "\n"
> ." <?php \n"
> ." if ($_GET['preview'] == \"y\") { \n"
> ." readfile(\"http://www.foo.bar/${'sitename'}.display?${'sitename'}id=$new_id&prev iew=y\"); \n"
> ." } else { \n"
> ." readfile(\"http://www.foo.bar/${'sitename'}.display?${'sitename'}id=$new_id\"); \n"
> ." } \n"
> ." ?>";
>
> write($new_file, $new_file_content);
> fclose($new_file);
>
> Note that $sitename is defined earlier in the script.
>
> I know that $new_id and $sitename get expanded, as I tried this:
>
> $new_file_content = "hello world. Look at my ${'sitename'}.display and my $new_id";
>
> and the new file was created with the above text and the two vars
> expanded. My problem lies in the fact that I need to put "<?php" tags
> inside the contents of $new_file_content. I'm not sure how to do that w/o
> generating lots of errors. As you can see in my above example, I've tried
> to escape the double quotes, but that didn't seem to help. PHP insists on
> processing the "if" line instead of ignoring it.
>
> I'm running 4.3.3 under apache 1.3.28.
>
> Any help would be most appreciated!
>
> /vjl/
>Bogdan Stancescu Guest
-
Erwin Moller #3
Re: using fwrite to create PHP files
Hi,
Ik think the parser is teasing you.
Try this workaround:
$myphpopentag = "";
$myphpopentag .= "<";
$myphpopentag .= "?php";
and use that $myphpopentag instead where needed.
Try the same for the closetag.
Hope that helps,
regards,
Erwin
Erwin Moller Guest
-
Chris Hubbard #4
RE: [PHP] using fwrite to create PHP files
Vince,
You also need to escape the $, so instead of
.." if ($_GET......
use
.." if (\$_GET.....
You may have to escape the single quotes.
I have written two pretty lame code generators. The first one did not use
"templates", the second one does. You've got the lines of code in your
"builder" script. I recommend moving those lines of code out, and put them
into a template, then use str_replace() to change the template as/where
needed.
chris
-----Original Message-----
From: Vince LaMonica [mailto:vjl@soceco.uci.edu]
Sent: Friday, September 05, 2003 1:20 AM
To: PHP-General
Subject: [PHP] using fwrite to create PHP files
Hi all,
I wish to use fwrite() to create a small PHP file. So far, when I attempt
to do this, php parses the contents of the file that fwrite needs to
create. Eg, I have this:
$new_id = mysql_insert_id();
// create brand new file
$new_file = fopen("/var/www/html/$sitename/$submitted_url", "w");
$new_file_content = "\n"
.." <?php \n"
.." if ($_GET['preview'] == \"y\") { \n"
.."
readfile(\"http://www.foo.bar/${'sitename'}.display?${'sitename'}id=$new_id&
preview=y\"); \n"
.." } else { \n"
.."
readfile(\"http://www.foo.bar/${'sitename'}.display?${'sitename'}id=$new_id\
"); \n"
.." } \n"
.." ?>";
write($new_file, $new_file_content);
fclose($new_file);
Note that $sitename is defined earlier in the script.
I know that $new_id and $sitename get expanded, as I tried this:
$new_file_content = "hello world. Look at my ${'sitename'}.display and my
$new_id";
and the new file was created with the above text and the two vars
expanded. My problem lies in the fact that I need to put "<?php" tags
inside the contents of $new_file_content. I'm not sure how to do that w/o
generating lots of errors. As you can see in my above example, I've tried
to escape the double quotes, but that didn't seem to help. PHP insists on
processing the "if" line instead of ignoring it.
I'm running 4.3.3 under apache 1.3.28.
Any help would be most appreciated!
/vjl/
--
Vince LaMonica UC Irvine, School of Social Ecology
W3 Developer <*> 116 Social Ecology I, Irvine, CA 92697
vjl@uci.edu http://www.seweb.uci.edu/techsupport
Tower: "Delta Zulu Romeo, turn right now and report your heading."
Pilot: "Wilco. 341, 342, 343, 344, 345..."
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Chris Hubbard Guest
-
John W. Holmes #5
Re: [PHP] using fwrite to create PHP files
Vince LaMonica wrote:
You need to escape the dollar signs in your string.> I wish to use fwrite() to create a small PHP file. So far, when I attempt
> to do this, php parses the contents of the file that fwrite needs to
> create. Eg, I have this:
>
> $new_id = mysql_insert_id();
> // create brand new file
> $new_file = fopen("/var/www/html/$sitename/$submitted_url", "w");
> $new_file_content = "\n"
> ." <?php \n"
> ." if ($_GET['preview'] == \"y\") { \n"
.." if (\$_GET['preview'] == \"y\") { \n"
--
---John Holmes...
Amazon Wishlist: [url]www.amazon.com/o/registry/3BEXC84AB3A5E/[/url]
php|architect: The Magazine for PHP Professionals – [url]www.phparch.com[/url]
John W. Holmes Guest
-
Chris Sherwood #6
Re: [PHP] using fwrite to create PHP files
this is a sample of what I do when I need to write a php file
$stringtowrite = "<?PHP\n// Bulletin Board
forum\n$"."ForumId=".$tabletofind.";\n";
$stringtowrite .=
"$"."ForumActive='1';\n$"."ForumName='".$sportname ."';\n";
$stringtowrite .= "$"."ForumDescription='".$sportname." forum';\n";
$stringtowrite .= "$"."ForumConfigSuffix='';\n";
$stringtowrite .= "$"."ForumFolder='0';\n";
$stringtowrite .= "$"."ForumParent='0';\n";
$stringtowrite .= "$"."ForumLang='lang/english.php';\n";
--- and so on until finally
$fd=fopen($final_destination,"w") or die ("file won't open");
/*if ($fd===false)
{
echo "file create failed";
exit();
//return;
} */
fwrite($fd,$stringtowrite);
fclose($fd);
<-- snip -->attempt> Vince LaMonica wrote:
>> > I wish to use fwrite() to create a small PHP file. So far, when I<-- snip -->>> > to do this, php parses the contents of the file that fwrite needs to
> > create. Eg, I have this:
> >
> > $new_id = mysql_insert_id();
> > // create brand new file
> > $new_file = fopen("/var/www/html/$sitename/$submitted_url", "w");
> > $new_file_content = "\n"
> > ." <?php \n"
> > ." if ($_GET['preview'] == \"y\") { \n"
> You need to escape the dollar signs in your string.
>
> ." if (\$_GET['preview'] == \"y\") { \n"
>
> --
> ---John Holmes...
Chris Sherwood
Chris Sherwood Guest



Reply With Quote

