我要写入和复制文件。而且我需要在写入和复制文件时echo
的值。
请检查我的代码:
<?php
$pages = array("story1", "story2", "story3","story4","story5","story6");
$length = count($pages);
for($i=0;$i<$length;$i++){
$myFile = "newfile-in-{$pages[$i]}.php";
if (file_exists($myFile)) {
echo copy("expcontent.php","newfile-in-{$pages[$i]}.php");
}else{
$myfile = fopen("newfile-in-{$pages[$i]}.php", "w") or die("Unable to open file!");
$txt = "Mickey {$pages[$i]}\n";
fwrite($myfile, $txt);
$txt = "Hello Mouse\n";
fwrite($myfile, $txt);
}
}
在我的expcontent.php
中:
<p>Kids <?php echo $pages[$i];?>!</p>
一切正常。新文件已创建并完美复制了文件。但是输出是:
Kids, !
Kids, !
Kids, !
Kids, !
Kids, !
预期输出:
Kids Story1!
Kids Story2!
Kids Story3!
Kids Story4!
Kids Story5!
Kids Story6!
我没有得到正确的输出,或者我认为$pages[$i]
上的expcontent.php
值。
请检查我的代码并纠正我。 任何想法或建议都将受到欢迎。 谢谢。
答案 0 :(得分:0)
通过阅读问题[除了问题]下的注释,我了解到您想通过写出文件内容而不是文件名来扩展故事。
如果您已创建故事的“章节”并将其存储为单独的文件,则可以使用 for (let i = 0; i <= locationInputs.length - 1; i++) {
try {
return await encryptionTool.decrypt(locationInputs[i], ciphertext, originalHash);
} catch (e) {
// Discard the exception and proceed to the next decryption attempt.
}
}
// TODO: Decide what to return here if all decryptions fail.
来写出内容:
例如
include
您可以将这些章节存储为.php,也可以使用备用扩展名,并阻止服务器配置文件(在Apache中<?php
$title="My Kids Story";
include "chapter1.php";
include "chapter2.php";
?>
)对[用户的直接访问]。 More about that here
例如将其放在您的配置中,并包含Chapter1.inc而不是.php
httpd.conf
答案 1 :(得分:-1)
您只是复制文件,但从未执行过。您可以读取文件内容并使用eval
执行它。请务必检查文档。
警告 eval()语言构造非常危险,因为它允许 执行任意PHP代码。因此不鼓励使用它。如果你 经过仔细验证,除了使用此功能外,没有其他选择 构造时,要特别注意不要传递任何用户提供的数据 在没有事先正确验证的情况下进入
。
$pages = array("story1", "story2", "story3","story4","story5","story6");
foreach ($pages as $page) {
$myFile = "newfile-in-{$page}.php";
if (file_exists($myFile)) {
$content = file_get_contents('expcontent.php');
$newcontent = eval($content);
file_put_contents($myFile, $newcontent);
} else {
$myfile = fopen("newfile-in-{$page}.php", "w") or die("Unable to open file!");
$txt = "Mickey {$page}\n";
fwrite($myfile, $txt);
$txt = "Hello Mouse\n";
fwrite($myfile, $txt);
}
}
再次从手册中:
不得将代码包装在打开和关闭PHP标记中。
因此,您必须将expcontent.php
更改为:
?><p>Kids <?php echo $page;?>!</p>
或
echo "<p>Kids {$page}!</p>";
在不使用eval的情况下,进行此操作的另一种更安全的方法是捕获第二页的输出(这仅是if块,它应与原始expcontent.php
一起使用)。
ob_start();
include 'expcontent.php';
$content = ob_get_clean();
file_put_contents($myFile, $content);