8

For part of my website I need to be able to write php code to a file with php. For example:

$filename = "RtestR.php";
$ourFileName =$filename;
$ourFileHandle = fopen($ourFileName, 'w');



$written =  "
    <html>
    <body>
    <?php
    echo \"I like the color \".$_SESSION['color'].\"!!!!\";
    </body>
    </html>

";

fwrite($ourFileHandle,$written);

fclose($ourFileHandle);

But, instead of creating the file, it throws this error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING on line 14

What am I doing wrong and what is the right way to write php code to a file?
EDIT:
I think I might need to make myself clearer... I want the SESSION to be determined when the newly created file is loaded. Technically I don't want to get the session on this page, but instead on the page I am creating!!! I want to write the code to the file, not the output of the code!

Machavity
  • 30,841
  • 27
  • 92
  • 100
pattyd
  • 5,927
  • 11
  • 38
  • 57

6 Answers6

9

Finally figured this out! I needed to escape my $ symbols! Like this:

$written =  "
<html>
<body>
<?php
echo \"I like the color \".\$_SESSION['color'].\"!!!!\";
</body>
</html>

";

Can't believe i didn't think of that ;)
Thank you all!

pattyd
  • 5,927
  • 11
  • 38
  • 57
  • Alternatively you could have used single quotes, like suggested by "nana.chorage". This has the added benefit of not having to escape all double quotes in the code you are generating. – Sven Jun 10 '13 at 19:54
  • Yes, but i still needed to change the $ – pattyd Jun 10 '13 at 21:43
  • 2
    No. Single quotes do not evaluate variables. `"$var"` will contain the content of `$var`, `"\$var"` will contain "dollar-var", as well as `'$var'`. The reason for the error with `"$_SESSION['color']"` is that the correct access to array values should either be `"$_SESSION[color]"` without quoting the index, or `"{$_SESSION['color']}"` with curly braces around the whole var expression. The curly brace style will work with any expression, including object properties. – Sven Jun 11 '13 at 07:30
1

You can do like this :-

    <?php

    $filename = "RtestR.php";
    $ourFileName =$filename;
    $ourFileHandle = fopen($ourFileName, 'w');



    $written =  "<html>
                    <body>          
                        I like the color ".$_SESSION['color']."!!!! 
                    </body>
                </html> ";

    fwrite($ourFileHandle,$written);

    fclose($ourFileHandle);

?>
Rakesh Shetty
  • 4,548
  • 7
  • 40
  • 79
1
$fp = fopen("test.php", "w");
$string = '<html>
    <body>
    <?php
    echo "I like the color ".$_SESSION[\'color\']."!!!!";
    ?>
    </body>
    </html>';

fwrite($fp, $string);

fclose($fp);
nana.chorage
  • 496
  • 4
  • 15
  • you need to do $fp before $string btw... also, that is practcally what i already have. sorry! – pattyd Jun 10 '13 at 18:10
1

It seems relevant to mention php's HEREDOC in this context, e.g.:

<?php
$filename      = 'RtestR.php';
$ourFileName   = $filename;
$ourFileHandle = fopen($ourFileName, 'w');

$write =  <<<"FILE_CONTENTS"
<p>I like the color <?={$cleanSessionVars[ 'color' ]};?>.</p>

FILE_CONTENTS;

fwrite($ourFileHandle, $write);

fclose($ourFileHandle);
imme
  • 598
  • 1
  • 9
  • 22
0

You're missing your closing PHP tag ?>.

Consider for a moment that what you are doing might not be the best approach anyway. The only use case I can think of for writing out PHP files with PHP would be for some compiled template code or weird caching.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • I have already considered my options... This is the only way i can do what I need to achieve, i tried to find other options and couldn't. Thanks though! – pattyd Jun 10 '13 at 18:09
  • You are not asking the right question then. Now we have a boring syntax error chase. But we could have an interesting hunt for the best solution. – Sven Jun 10 '13 at 19:46
0

Hello pattyd: Nice to see ya :) + Don't upvote/accept this answer:

I would suggest simplified in this way:

$written =  "
    <html>
        <body>
           I like the color \" $_SESSION['color'] \" !!!!
        </body>
    </html>
";
  • Hey! I think i might need to make myself clearer... I want the SESSION to be determined when the newly created file is loaded.... so technically i dont want to get the session on this page, but instead on the page i am creating! Does that make sense, or do you know what i mean? thanks! – pattyd Jun 10 '13 at 18:07
  • Somebody +1 this for me ;) – pattyd Jun 10 '13 at 18:11
  • :) Well then, what is the problem? If the session 'color' was present at run time, it would write the it to the file, just as you are opening the page. So, just make sure the session is set. –  Jun 10 '13 at 18:12
  • Wait, are you kidding me? That is **all** i have to do? Woah... so then it writes the code, not the output right? So that the code will be interpreted on the newly created page? If that is so, you just saved my day :) Is that what you mean? – pattyd Jun 10 '13 at 18:13
  • Yes! Although, I don't remember ever creating/writing to files. I am saying, that is what it is. Just try it, and if it does not work out, or you couldn't get help here. Ping me in the room. We'll check it together later –  Jun 10 '13 at 18:17
  • Good plan, will check this ASAP – pattyd Jun 10 '13 at 18:24
  • As it turned out, this solution will write the session variable at the time of file creation, not execution - which is not the same. – Sven Jun 10 '13 at 19:53