How to Create a Comma-Separated Values (CSV) File Using PHP


In this article, we show how to create a comma-separated values (CSV) file using PHP.

A CSV file, also called a comma-delimited file, is a common file or format used to transfer information between various databases.

It can kind of be looked at as a platform-independent file that stores rows and columns of a table. This file, in turn, can be read by various databases such as Microsoft Excel, MySQL, and Microsoft Access. Thus, one file written, a CSV file, can interact with many different databases, with no need to modify the code at all. So it's like, write once, run everywhere.

This is why CSV files are a popular format for working with data that goes into tables of databases. If working with many databases, it serves as a platform-neutral choice to minimize or completely get rid of the need to modify code.

You can write a CSV file by hand or you can use PHP to create a CSV file.

We can then later load this file into a MySQL table so that this data can be added to the table.

PHP Code

The PHP code to create a CSV file is shown below.

The code above creates a CSV based on arrays, which is probably the easiest way to code rows in PHP.

The code creates a CSV file for users. The data entered in is the user's name, age, and occupation. There are 4 rows entered in, representing 4 users. For each user, there are 3 columns of information entered, the name, age, and occupation of the user.

We take this data and create a file named "users.txt". If the file already exists, then the file is opened and we append this data to it. A CSV file can be of many formats. You can make it be a plain text file or you can create a CSV file built by commercial companies such as Excel by Microsoft. PHP can work with a variety of file formats, formatted properly in CSV. Thus, if you change the file above to "users.csv", it creates a special csv file extension that can be used and opened by Microsoft Excel. Thus, PHP is very dynamic. It can read or create several different CSV formatted files.

We create a variable named $arraycount that counts the number of array elements created. The reason we do this is so that you don't have to manually count the elements yourself. If you are creating a lot, you probably won't want to count it. You'll need to know the amount because in the for loop we create which goes through all the array elements you created, we need to loop through all the elements and the only way we can do this is by knowing how many elements exist in the array.

For each element in the array, we write the array element using the PHP fwrite() function. We add in "\n" to create a line break between each array. Line breaks are very important when coding CSVs. With them, it allows us to know each distinctive row of data. Without line breaks, the code will not be in proper CSV format and will not work.

After we have looped through all elements in the array and have written them all out into our file we have specified and created, we then close the file (since we're finished writing to it).

Running the example, a file is created named users.txt, which is shown at the following link: users.txt.

If you run the example above and change users.txt to users.csv, the following file is created: users.csv.

You can now use this CSV file and load it directly into a MySQL database so that new rows of data can be inserted into a MySQL table. To see how to do this, see How to Load a CSV File into a MySQL Table Using PHP.

Related Resources

How to Load a CSV File into a MySQL Table Using PHP

HTML Comment Box is loading comments...