在 eod 中插入特定元素

insert specific element inside eod

安装时需要在文件中注入代码

结果必须是

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;

您必须在 heredoc 字符串中转义美元符号字符 $,或者使用 nowdoc 字符串代替

Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a 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)
}