Answer the question
In order to leave comments, you need to log in
How to generate an XML file and immediately download it without saving it?
There is an example that generates xml, saves it and then loads it.
$dom = new DomDocument('1.0');
$books = $dom->appendChild($dom->createElement('books'));
$book = $books->appendChild($dom->createElement('book'));
$title = $book->appendChild($dom->createElement('title'));
$title->appendChild(
$dom->createTextNode('Great American Novel'));
$dom->formatOutput = true; // установка атрибута formatOutput
$dom->save('test1.xml');
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="test1.xml"');
Answer the question
In order to leave comments, you need to log in
$dom = new DomDocument('1.0');
$books = $dom->appendChild($dom->createElement('books'));
$book = $books->appendChild($dom->createElement('book'));
$title = $book->appendChild($dom->createElement('title'));
$title->appendChild(
$dom->createTextNode('Great American Novel'));
$dom->formatOutput = true; // установка атрибута formatOutput
header('Content-Type: application/octet-stream');
echo $dom->saveXML();
function forceDownload($xml)
{
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/xml");
header("Content-Disposition: attachment; filename=" . basename($xml) . ";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($xml));
readfile($xml);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question