试图用PHP构建一个文件并使用fwrite来存档

时间:2016-06-07 21:26:48

标签: php fwrite

尝试弄清楚我如何制作并保存文件,同时包含来自其他文件的默认信息。我试过包括但它没有用。有关如何构建此文件的任何建议吗?

<?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);
?>

1 个答案:

答案 0 :(得分:0)

我没有在第一段代码中看到问题。

在第二个块上,解释器会将第二个双引号视为字符串$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对你意味着什么。它应该是评估真或假的条件。