Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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
将格式化的xml保存到PHP中的文件_Php_Xml_Domdocument - Fatal编程技术网

将格式化的xml保存到PHP中的文件

将格式化的xml保存到PHP中的文件,php,xml,domdocument,Php,Xml,Domdocument,我知道这里有很多关于这个的问题,但我尝试的每个答案都没有解决我的问题 问题 我将xml保存在一个字符串上,我希望将其保存到一个文件中,但不希望xml位于一行中,我希望以pritty打印方式写入该文件 还有一个更大的问题,它显示了这个“”,而不是这个“,因为这个文件将被加载到不同的网站,我不能有” 请注意,$content变量具有“而不是” 我知道\n并不重要,但为了更容易分析生成的xml文件,我需要格式化xml 这就是我正在使用的 $dirname = "temp/" . uniqid(); m

我知道这里有很多关于这个的问题,但我尝试的每个答案都没有解决我的问题

问题

我将xml保存在一个字符串上,我希望将其保存到一个文件中,但不希望xml位于一行中,我希望以pritty打印方式写入该文件

还有一个更大的问题,它显示了这个
”,而不是这个
,因为这个文件将被加载到不同的网站,我不能有

请注意,
$content
变量具有
而不是

我知道
\n
并不重要,但为了更容易分析生成的xml文件,我需要格式化xml

这就是我正在使用的

$dirname = "temp/" . uniqid();
mkdir($dirname);

$filename = $dirname . "/gen.xml";

$doc = new DomDocument($content);
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$doc->save($filename);

header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=gen.xml");
header("Content-Type: application/octet-stream; "); 
header("Content-Transfer-Encoding: binary");

readfile($filename);

$fh = fopen($filename, 'a');
fclose($fh);
unlink($filename);
rmdir($dirname);
我也试过了

标题(“内容类型:text/xml”);

file\u put\u contents

DomDocument
不接受xml字符串,而是可选版本和编码

您可能希望加载
$content
xml字符串:

$doc = new DOMDocument();

$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$doc->loadXML($content);

$doc->save($filename);