Search code examples
phpdomiosavedocuments

Generate file in PHP then reference it, without storing it on the server?


Basically what I'm doing is generating a License.xml file based on information from my database, random generation, and some other factors, based on my research I was under the impression that if I put this in my code, it would automatically download the License.xml file to the users computer.

header('Content-Disposition: attachment;filename=License.xml');
header('Content-Type: text/xml');
$dom = new DOMDocument();
$xml = $dom->createTextNode('<Account Username="'.$fuser.'" Password="'.$fpass.'" Key="'.$softwareKey.'"></Account>');
$xml = $dom->appendChild($xml);
echo $dom->saveXML();

However, this is not the case-- Actually what this does is makes it so the page doesn't load, and instead the entire generated source is saved as License.xml

The functionality that I'm after is that the user will be able to load the page without any problems, and view content on the page, however this file will automatically download when the conditions are met.

If this isn't possible, then is it possible to store it in a link value, for example:

Click <a href="#">here</a> to download your License file!

The other thing that I want to be able to do is attatch the file to an e-mail, but I've found numerous explanations on how to do that, however apparently I'm not one for generating files, I don't understand how DOM works and no matter how many tutorials, etc I read.. It just hurts my head, so I decided to just parse the entire XML file as text. (Not like it matters, this is the exact format my application reads the License.XML file in anyway )

Any help is much appreciated, please don't link me to the DOM Documents I've been staring at them for two hours and it hurts my head, the entire official document page for PHP just gives me a headache.


Solution

  • Hi most likely you have something like this

    <p> some content </p>
    <?php //hopefully a php tag. etc.
    header('Content-Disposition: attachment;filename=License.xml');
    header('Content-Type: text/xml');
    ....  download code ....
    ?>
    <p> some more content </p>
    

    This wont work, because you cannot have output before a header call. The way around it is to make a separate file. Pass this some type of validation token and run your code in an iframe or as a separate request ( separate web page ). You might also have content after the code in which case it will be included in you downloaded file.

    The header basically sets what type of page it is. By default most webservers have a header of Content-Type: text/html;. Essentially you can't mix a download with html content. Unless you want to download that html content too. You can't call header after output because the webserver has already sent this default html header if you do any outputting.

    Ok, the simplest way is put you're download code in a separate file, we'll call it download.php

    then point a link to it.

    <a href="location of download.php" >click me</a>
    

    When it loads it will run the code. Just make sure not to output anything before the header( ) function. Not even a blank line before the <?php tag

    An Iframe will do it without the link you just do like this

    <iframe src="location of download.php" style="display:none;" ></iframe>
    

    Think of an iframe like a portal to another page.