How to Convert CSV to XML using PHP

The example, convert csv to xml using PHP , shows conversion of csv data to xml data. As the title suggests to convert csv to xml using PHP, so we will convert either csv file to xml file or csv string to xml string. When we convert csv string to xml string you will see the output on the browser and when we convert csv file to xml file then you will see the xml document saved into a file on the physical drive location. So here in this example we will see both examples that may help you to choose the example according to your situations.

People working with different types of data may come across the problem that they need to convert between different data formats. Comma Separated Values (CSV) and the Extensible Markup Language (XML) are the most widely used formats for data, and conversion between these two formats needs often to be accomplished. Especially to XML, because this format is very well supported by modern applications, and is very well suited for further data manipulation and customization.

Continue reading “How to Convert CSV to XML using PHP”

Read from and write to a csv file using php

This tutorial shows how to read from csv file and write to a csv file using php. In this example I have shown how to read a csv file and display those values. I have also written those csv entries to another csv file.

Code snippets for reading a csv file:

The below code snippets first check if the file exists and has required permission for reading.

Then we need to iterate for each line and print the data using array index. The fgetcsv function retrieves comma separated values into array from each line.

$file_name = "read.csv";
if (($handle = fopen($file_name, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
        echo $data[0] or $data[1] or $data[2] etc.
        //more code


Continue reading “Read from and write to a csv file using php”