xml - How to create a file from PHP echoed content? -
this question has answer here:
- saving php output in file 2 answers
i want execute php script , create xml file. php script contains set of 'echo' commands.
$name = strftime('xml_%m_%d_%y.xml'); header('content-disposition: attachment;filename=' . $name); header('content-type: text/xml'); echo '<?xml version="1.0" encoding="utf-8"?>'.php_eol; echo '<lager>'.php_eol; foreach ($product_array $product) { echo "<product>".php_eol; echo "<code>", $product->var_code, "</code>".php_eol; echo "<group>", $product->var_group, "</group>".php_eol; echo "<manu>", $product->var_manufacturer, "</manu>".php_eol; echo "<product>".php_eol; } echo '</lager>'.php_eol;
i did script, browser starts download file. instead of that, want script create file , keep @ server.
- if don't need send file user (/browser) don't need header functions. these headers exists tell browser type of content server provides.
- if want save file in server - can use of
file_put_contents
orfopen
+fwrite
functions so.
here code looking for. fixed </product>
(added slash missed there, @marten koetsier)
$name = strftime('xml_%m_%d_%y.xml'); // open file writing $fp = fopen($name, 'w'); fwrite($fp, '<?xml version="1.0" encoding="utf-8"?>'); fwrite($fp, '<lager>'); foreach ($product_array $product) { fwrite($fp, "<product>"); fwrite($fp, "<code>{$product->var_code}</code>"); fwrite($fp, "<group>{$product->var_group}</group>"); fwrite($fp, "<manu>{$product->var_manufacturer}</manu>"); fwrite($fp, "</product>"); } fwrite($fp, '</lager>'); fclose($fp);
and since save xml don't need php_eol
. can put if want...
Comments
Post a Comment