Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从PHP回显内容创建文件?_Php_Xml - Fatal编程技术网

如何从PHP回显内容创建文件?

如何从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

我想执行一个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>'.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;
我已经这样做了,但是通过这个脚本,浏览器开始下载一个文件。 相反,我希望脚本创建文件并保存在服务器上

  • 如果不需要将文件发送给用户(/browser),则不需要头函数。这些标题的存在只是为了告诉浏览器服务器提供的内容类型
  • 如果要在服务器中保存文件,可以使用
    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);