如何使用php将php代码写入文件

如何使用php将php代码写入文件

本文介绍了如何使用php将php代码写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的网站的一部分,我需要能够使用php将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:

我做错了什么,将php代码写入文件的正确方法是什么?

我认为我可能需要使自己更加清晰...我希望在加载新创建的文件时确定SESSION.从技术上讲,我不想在此页面上获取会话,而是在我正在创建的页面上!我想将代码写入文件,而不是代码的输出!

What am I doing wrong and what is the right way to write php code to a file?

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!

推荐答案

终于明白了!我需要转义$符号!像这样:

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!

这篇关于如何使用php将php代码写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:24