CSV到XML - 当data.csv位于文件夹外部时,XML-import.xml保存文件为空 [英] CSV to XML - when the data.csv is outside from the folder the XML-import.xml save file is empty
问题描述
当我将'data.csv'移动到其他位置时,它不会在我的计算机上保存'xml-import.xml'。该代码仅在CSV文件'data.csv'与convert.php(我的PHP脚本的名称)位于同一文件夹中时有效,但如果您将data.csv移动到其他位置,则'xml-import.xml'是不保存。
即使您从不同位置移动data.csv文件,'xml-import.xml'也保存,我所需要的只是。这可能吗?
我尝试过:
When I moved the 'data.csv' to other place location it will not save the 'xml-import.xml' on my computer. The code only works if the CSV file 'data.csv' is in the same folder with convert.php (name of my PHP script) but if you moved the data.csv to different location, the 'xml-import.xml' are not save.
All I need is even if you move the data.csv files from different location the 'xml-import.xml' also save. Is that possible?
What I have tried:
<form action="" method="POST">
<input id="my-file-selector" type="file" name="fname" value="Choose File to Upload">
<input class="btn btn-warning" type="submit" name="submit" value="CONVERT" >
</form>
<?php
error_reporting(0);
if(isset($_POST['fname'])){
$value = $_POST['fname'];
}
//$rows = array_map('str_getcsv', file('./PATH/$value'));
$rows = array_map('str_getcsv', file($value));
$header = array_shift($rows);
$data = array();
foreach ($rows as $row)
{
$data[] = array_combine($header, $row);
}
foreach($data AS $key => $val)
{
}
$xml = new DomDocument('1.0', 'UTF-8');
$root = $xml->createElement('root');
$xml->appendChild($root);
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);
$name = $entry->appendChild($xml->createElement($field_name));
$name->appendChild($xml->createCDATASection($field_value));
}
}
$xml->formatOutput = true;
$xml->save('xml-import.xml');
?>
推荐答案
_POST ['fname'])){
_POST['fname'])){
value =
_POST ['fname'];
}
//
_POST['fname']; } //
这篇关于CSV到XML - 当data.csv位于文件夹外部时,XML-import.xml保存文件为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!