Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

fwrite - how to write php code to a file with php

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...