尝试在 PHP 中构建文件并使用 fwrite 来归档

Trying to build a file in PHP and use fwrite to file

试图找出如何在包含另一个文件的默认信息的同时制作和保存文件。我试过包括但没有用。关于如何构建此文件的任何建议?

<?php
$wrtID = $_POST["fileID"];

SQL statement here to get relevant info

mkdir("CNC/$wrtID", 0770, true);

?>
<?php

$batfile = fopen("CNC/$wrtID/$wrtID.bat", "w") or die("Unable to open file!");
$txt = "
@ECHO OFF
@ECHO **** Run NC-Generator WOODWOP 4.0 ****
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-sl.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-sr.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-tb.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-dc.mpr
@ECHO **** Done ****
";
fwrite($batfile, $txt);
fclose($batfile);
?>

<?php
$slfile = fopen("CNC/$wrtID/$wrtID-sl.mpr", "w") or die("Unable to open file!");
$txt = "

include("defaultcnc.php");

if ( additional file pats needed ) {
    include("component-1.php");
}

";

fwrite($slfile, $txt);
fclose($slfile);
?>

我在第一段代码中没有发现问题。

在第二个块中,解释器会将第二个双引号视为字符串的结尾 $txt = " include("。所以之后的一切都会产生 PHP 错误。

但即使您转义了这些文件,mpr 文件也将包含字符串 include("defaultcnc,php"); 而不是该文件的实际内容。为此你应该做 file_get_contents("defaultcnc.php").

类似于:

<?php
$slfile = fopen("CNC/$wrtID/$wrtID-sl.mpr", "w") or die("Unable to open file!");
// set params to pass to defaultcnc.php
$value1 = 1;
$value2 = "I'm a text string";
$file = urlencode("defaultcnc.php?key1=".$value1."&key2=".$value2);
$txt = file_get_contents($file);

if ( additional file pats needed ) {
    $txt .= file_get_contents("component-1.php");
}

fwrite($slfile, $txt);
fclose($slfile);
?>

我想 additional file pats needed 对你来说意味着什么。它应该是评估真或假的条件。