如何从PHP回显内容创建文件?
我想执行一个PHP脚本并创建一个XML文件。PHP脚本包含一组“echo”命令如何从PHP回显内容创建文件?,php,xml,Php,Xml,我想执行一个PHP脚本并创建一个XML文件。PHP脚本包含一组“echo”命令 $name = strftime('xml_%m_%d_%Y.xml'); header('Content-Disposition: attachment;filename=' . $name); header('Content-Type: text/xml'); echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; echo '<lager
$name = strftime('xml_%m_%d_%Y.xml');
header('Content-Disposition: attachment;filename=' . $name);
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
echo '<lager>'.PHP_EOL;
foreach ($product_array as $product) {
echo "<product>".PHP_EOL;
echo "<code>", $product->var_code, "</code>".PHP_EOL;
echo "<group>", $product->var_group, "</group>".PHP_EOL;
echo "<manu>", $product->var_manufacturer, "</manu>".PHP_EOL;
echo "<product>".PHP_EOL;
}
echo '</lager>'.PHP_EOL;
我已经这样做了,但是通过这个脚本,浏览器开始下载一个文件。
相反,我希望脚本创建文件并保存在服务器上
file\u put\u contents
或fopen
+fwrite
功能进行保存
(由于@Marten Koetsier,添加了您在那里错过的斜杠)
由于您将其保存为XML,因此实际上不需要
PHP\u EOL
。如果你真的想要…确实是一个副本,你可以把我放回去。使用输出缓冲作为[回答]@MartenKoetsier我的输出大约为8-9MB,我尝试了这个,但5-6分钟后我收到了内部服务器错误消息。是否有一个增加最大执行时间的解决方案?@Caci:假设这运行了某个服务器,你能检查服务器的错误日志吗?如果这给出了最大执行时间的错误,它还会记录最大执行时间是多少。通常这是30秒(apache默认值)。在这种情况下,这可能会解决您的问题。哦,输出代码中有一个错误:foreach
中的最后一个回音应该是一个结束标记:
(您错过了斜杠!)(顺便说一句。这是关于啤酒的吗?很好!)
$name = strftime('xml_%m_%d_%Y.xml');
// Open the file for writing
$fp = fopen($name, 'w');
fwrite($fp, '<?xml version="1.0" encoding="UTF-8"?>');
fwrite($fp, '<lager>');
foreach ($product_array as $product) {
fwrite($fp, "<product>");
fwrite($fp, "<code>{$product->var_code}</code>");
fwrite($fp, "<group>{$product->var_group}</group>");
fwrite($fp, "<manu>{$product->var_manufacturer}</manu>");
fwrite($fp, "</product>");
}
fwrite($fp, '</lager>');
fclose($fp);