简体   繁体   中英

How can I export to an XML file instead of printing it on screen?

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;
?> 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM