using fwrite to create PHP files

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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")...
    2. [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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default 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

  4. #3

    Default 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

  5. #4

    Default 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

  6. #5

    Default Re: [PHP] using fwrite to create PHP files

    Vince LaMonica wrote:
    > 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"
    You need to escape the dollar signs in your string.

    .." 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

  7. #6

    Default 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 -->
    > Vince LaMonica wrote:
    >
    > > 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"
    >
    > You need to escape the dollar signs in your string.
    >
    > ." if (\$_GET['preview'] == \"y\") { \n"
    >
    > --
    > ---John Holmes...
    <-- snip -->

    Chris Sherwood
    Chris Sherwood Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139