编写PHP文件不起作用 [英] Writing PHP file not working

查看:109
本文介绍了编写PHP文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用fwrite()函数动态编写PHP文件,但似乎无法正常工作.该代码肯定在所写的字符串中,但是当我打开所写的文件时,它就不存在了!我的假设是,无论出于何种原因,PHP编译器都会读取字符串中的PHP标记,执行它们之间的内容,然后放弃它们.这个假设正确吗?我该怎么办才能解决这个问题?

I'm trying to dynamically write PHP files using the fwrite() function and it just does not seem to be working. The code is definitely in the string being written, but then when I open the file that was written it just isn't there! My assumption is that the PHP compiler is, for whatever reason, reading the PHP tags in the string, executing what's between them, and then abandoning them. Is this assumption accurate? What can I do to get around it?

代码看起来像这样:

$code = "<?php echo \"This is a dynamically written file!\"; ?>";
$openFile = fopen("filename.php","w");
fwrite($openFile,$code);
fclose($openFile);

我尝试在'code'变量周围使用单引号和双引号.

I have tried both single and double quotes around the 'code' variable.

我尝试使用单引号,但随后将单引号变量与双引号变量混合并进行了转换.我觉得很蠢.很抱歉浪费大家的时间.

I tried single quotes, but then the single-quoted variable was mixing with a double-quoted variable and converting it. I feel dumb. Sorry for wasting everybody's time.

推荐答案

您需要在字符串周围加上乱七八糟的引号.您不希望PHP对其进行解析,而您则希望该字符串.此外,还有另一个功能, file_put_contents() :

You need to put litteral quotes around the string. You dont want PHP to parse it, you want litterly that string. Also, there is another function for that, file_put_contents():

$code = '<?php echo "This is a dynamically written file!"; ?>';
$openFile = file_put_contents("filename.php");

此函数与依次调用fopen(),fwrite()和fclose()来将数据写入文件相同.

This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.

当心:您正在玩一个危险的游戏,正在编写其他PHP文件.您最好110%确定自己在做什么,并仔细检查每一步.无论您在做什么,都有一种更安全的方法.

Be ware: You're playing a dangerous game, writing other PHP files. You better be 110% sure what you're doing and double/triple check every step you take. Whatever it is you're doing, there is a safer way.

这篇关于编写PHP文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆