Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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');
?>
Posted
Updated 3-Oct-18 22:24pm
v2
Comments
Richard MacCutchan 1-Oct-18 12:15pm    
You need to ensure that you provide the full path to the file if it is not in the current (for PHP) working directory.
Member 14003868 1-Oct-18 22:29pm    
sir. can u help me how to do the full path? my php script convert.php is uploaded in the server but the data.csv is on my local computer its separate. it that possible to get the path from my local computer? because my php script convert.php is uploaded on the server..
Richard MacCutchan 2-Oct-18 4:47am    
You cannot access a file on the client direct from the server. You need to upload the file to the server first in order to process it.

1 solution

PHP: POST method uploads - Manual[^]
PHP 5 File Upload[^]

You're trying to open the file given its path on the client. You need to open the file using the temporary path on the server where the uploaded file was saved.

Replace:
PHP
if(isset($_POST['fname'])){
    $value = $_POST['fname'];
}
with:
PHP
$value = $_FILES["fname"]["tmp_name"];
 
Share this answer
 
Comments
Member 14003868 4-Oct-18 4:24am    
Need help sir, I want to automatically download an XML format files from my computer or in download folder location inside.. Now its only saving on my working directory the xml converted file, Is that possible Automatically Download save after converted into XML?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900