在eod中插入特定元素

问题描述 投票:0回答:1

安装时我需要在文件内插入代码

结果必须为

if (isset($_SESSION['admin']['id'])) {
  define('DIR_FS_IMAGES', '{$dir_fs_document_root}images/');  
  define('DIR_WS_IMAGES', '{$http_catalog}images/'); 
}

在进程安装过程中,我尝试了此操作,但如果看起来不太正常。上面的结果如何处理?

谢谢

$file_contents = <<<ENDCFG
<?php
if (isset($_SESSION['admin']['id'])) {
define('DIR_FS_IMAGES', '{$dir_fs_document_root}/images/');  // path to files (REQUIRED)
define('DIR_WS_IMAGES', '{$http_catalog}/images/'); // URL to files (REQUIRED)
}
ENDCFG;
php heredoc
1个回答
0
投票

您必须在Heredoc字符串中转义美元符号字符$,或改用nowdoc字符串

Nowdocs是单引号字符串,heredocs是双引号字符串。 nowdoc的指定方式与Heredoc相似,但是nowdoc内部没有进行解析。

<?php
$file_contents = <<<'ENDCFG'
<?php
if (isset($_SESSION['admin']['id'])) {
define('DIR_FS_IMAGES', '{$dir_fs_document_root}/images/');  // path to files (REQUIRED)
define('DIR_WS_IMAGES', '{$http_catalog}/images/'); // URL to files (REQUIRED)
}
ENDCFG;

file_put_contents('/tmp/out.php', $file_contents);

结果

$ php test.php
$ file out.php
out.php: PHP script text, ASCII text
$ cat out.php
<?php
if (isset($_SESSION['admin']['id'])) {
define('DIR_FS_IMAGES', '{$dir_fs_document_root}/images/');  // path to files (REQUIRED)
define('DIR_WS_IMAGES', '{$http_catalog}/images/'); // URL to files (REQUIRED)
}
© www.soinside.com 2019 - 2024. All rights reserved.