[英]How to Download Automatically an xml format file direct from my computer or in download folder after converted into CSV TO XML
先生,我需要幫助,我想從我的計算機或下載文件夾中自動下載XML格式的文件。.現在,它僅將xml轉換后的文件保存在我的工作目錄中,請問轉換為XML后是否可以自動下載保存?
<?php
// Map CSV file to array
$rows = array_map('str_getcsv', file('data.csv'));
$header = array_shift($rows);
$data = array();
foreach ($rows as $row)
{
$data[] = array_combine($header, $row);
}
// Process Data if need be
foreach($data AS $key => $val)
{
// Processing here
}
//Creates XML string and XML document using the DOM
$xml = new DomDocument('1.0', 'UTF-8');
//Add root node
$root = $xml->createElement('root');
$xml->appendChild($root);
// Add child nodes
foreach($data AS $key => $val)
{
$entry = $xml->createElement('entry');
$root->appendChild($entry);
foreach($val AS $field_name => $field_value)
{
$field_name = preg_replace("/[^A-Za-z0-9]/", '', $field_name); // preg_replace has the allowed characters
$name = $entry->appendChild($xml->createElement($field_name));
$name->appendChild($xml->createCDATASection($field_value));
}
}
// Set the formatOutput attribute of xml to true
$xml->formatOutput = true;
// Output to screen
//header('Content-Type: text/xml');
//echo $xml->saveXML();
// Save as file
$xml->save('xml-import.xml'); // save as file
?>
DomDocument有一個saveXML
方法,該方法返回一個可以傳遞給瀏覽器的字符串。 http://php.net/manual/de/domdocument.savexml.php
$str_xml = $xml->saveXML();
echo $str_xml;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.