If you do save in the same php file, then I do like this

$xmlFileName = 'test2.xml'; $xmlData = $xml->asXML(); $xmlLength = strlen($xmlData); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.$xmlFileName); header('Content-Length: '.$xmlLength); echo $xmlData 

But in this file I also have the output of the same document, then the save will not work. How can I transfer the $ xmlData variable with the xml document to another file?

The correct option will be to save the file on the server, then send a link with GET with the name of the file, will you upload this file, send it to a computer for saving, and then delete it on the server?

    1 answer 1

    Scripts in PHP can return values. You can make return $xmlData instead of echo $xmlData .
    In the place where you need to display the result of the script, make echo require '/path/to/script/script.php' , and where you need to do something instead of output, just $xml = require '/path/to/script/script.php'

    • This is just hellish practice. - Ipatiev
    • If a person works in a procedural style, he does not have object architecture and everything is tied up in separate scripts. This practice occurs, and quite often. Of course, we can say that he rewrote everything, but is there any point in this? Perhaps he needs this script for personal one-time use. So I consider your minus unreasonable. If you look, many frameworks work this way. In one script, the object is configured and formed. In another script, it connects in this way. - Igor