[PHP] using fwrite to create PHP files

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

  1. #1

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

    [snip]
    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:
    [/snip]

    Vince I did this

    <?php
    /*
    ** Write PHP with PHP
    ** 2003-09-05
    */

    $newphp = fopen("/usr/server/test/new_php.php", "w");

    fputs($newphp, "<?php\n");
    fputs($newphp, "This is a PHP by PHP writing test.\n");
    fputs($newphp, "?>\n");

    fclose($newphp);
    ?>

    And it worked fine. fputs() places the string in the file properly. I
    also noticed at the end of your script....

    write($new_file, $new_file_content);

    Since I saw no write() function in your script I would have to assume
    that you are calling an undefined function, which is probably where your
    errors are coming from. Try changing the line to

    fputs($new_file, $new_file_content);

    And you should have much luck. Have a pleasant, productive and
    meaningful day.
    Jay Blanchard 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. 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...
    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: [PHP] using fwrite to create PHP files

    On 05 September 2003 19:44, Chris Sherwood wrote:
    > 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);
    Ouch! This looks like a prime candidate for a heredoc ([url]http://www.php.net/language.types.string#language.types.string.syntax .heredoc):[/url]

    $stringtowrite = <<<STRINGTOWRITE
    // Bulletin Board forum
    \$ForumId=$tabletofind;
    \$ForumActive='1';
    \$ForumName='$sportname';
    \$ForumDescription='$sportname forum';
    \$ForumConfigSuffix='';
    \$ForumFolder='0';
    \$ForumParent='0';
    \$ForumLang='lang/english.php';
    STRINGTOWRITE;

    You can even embed the heredoc directly in the fwrite():

    $fd=fopen($final_destination,"w") or die ("file won't open");
    fwrite($fd, <<<FILECONTENT
    // Bulletin Board forum
    \$ForumId=$tabletofind;
    \$ForumActive='1';
    \$ForumName='$sportname';
    \$ForumDescription='$sportname forum';
    \$ForumConfigSuffix='';
    \$ForumFolder='0';
    \$ForumParent='0';
    \$ForumLang='lang/english.php';
    FILECONTENT
    );
    fclose($fd);

    Cheers!

    Mike

    ---------------------------------------------------------------------
    Mike Ford, Electronic Information Services Adviser,
    Learning Support Services, Learning & Information Services,
    JG125, James Graham Building, Leeds Metropolitan University,
    Beckett Park, LEEDS, LS6 3QS, United Kingdom
    Email: [email]m.ford@lmu.ac.uk[/email]
    Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
    Mike Ford 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