我正在尝试在我的服务器上创建一个用于创建PHP文件的PHP脚本,但是我遇到了这个错误:
Parse error: syntax error, unexpected T_STRING in /home/judiesst/public_html/77/create.php on line 3
这里是用于在服务器上创建php文件的php scipt
<?php
$filename = 'test.php';
$somecontent = "<?php $jdst_xx = "HELLO"; ?>\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
///而另一个问题是 当我运行这个脚本(创建普通文本文件[不是php脚本])时它可以工作但下次当我再次运行这个脚本时,输出文件中的内容与之前的内容合并,所以我希望脚本重写输出文件中的所有内容,而不是与之前的内容合并,但我不知道该怎么做。
答案 0 :(得分:3)
更改
$somecontent = "<?php $jdst_xx = "HELLO"; ?>\n";
要
$somecontent = "<?php $jdst_xx = 'HELLO'; ?>\n";
答案 1 :(得分:0)
当你在双引号内有双引号时,你需要转义它们或用单引号替换它们。
所以你可以使用这样的东西
$somecontent = "<?php $jdst_xx = \"HELLO\"; ?>\n";
或
$somecontent = "<?php $jdst_xx = 'HELLO'; ?>\n";