T
T
tigra2016-09-10 22:38:52
PHP
tigra, 2016-09-10 22:38:52

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"');

How to make sure that the file is not saved, but immediately after the generation, the download takes place. Perhaps it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
CyberHost, 2016-09-10
@tigroid3

$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();

P
pingo, 2016-09-10
@pingo

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 question

Ask a Question

731 491 924 answers to any question