Reputation: 8836
How can I edit the code I use below to save the generated file instead of printing it on the screen ?
<?php
header("Content-type: text/xml");
$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");
$query = "";
$resultID = mysql_query($query, $linkID) or die("Data not found.");
$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<products>\n";
for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
$row = mysql_fetch_assoc($resultID);
$xml_output .= "\t<product>\n";
.
.
.
$xml_output .= "\t</product>\n";
}
$xml_output .= "</products>";
echo $xml_output;
?>
Upvotes: 0
Views: 7770
Reputation: 710
You would use the "Content-Disposition" header to provide a recommended filename and force the web browser to show the save dialog.
For example:
<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="downloaded.xml"');
// ...your other code
?>
This isn't guaranteed to work on all server setups and all browsers so you will find further information on the PHP header function page: http://php.net/manual/en/function.header.php
Upvotes: 3