PHP CSV (original) (raw)
Summary: in this tutorial, you will learn how to deal with CSV files in PHP, including creating and reading CSV files.
A quick introduction to CSV files #
CSV stands for comma-separated values. A CSV file is a text file that stores tabular data in the form of comma-separated values. A CSV file stores each record per line. And it may have a header.
When you open a CSV file using a spreadsheet application, you’ll see that the file is nicely formatted like this:
However, if you view the CSV file in a text editor, it looks like the following:
Symbol,Company,Price GOOG,"Google Inc.",800 AAPL,"Apple Inc.",500 AMZN,"Amazon.com Inc.",250 YHOO,"Yahoo! Inc.",250 FB,"Facebook, Inc.",30
Code language: plaintext (plaintext)
Typically, a CSV file uses a comma (,
) to separate fields in a CSV file. If the field content also contains a comma(,
), the CSV file surrounds that field with double quotes, e.g., “Facebook, Inc..”
Besides using the comma (,
) character, a CSV file may use other characters to separate fields such as semicolon (;
).
Writing to a CSV file #
To write a line to a CSV file, you use the fputcsv()
function:
fputcsv ( resource <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>h</mi><mi>a</mi><mi>n</mi><mi>d</mi><mi>l</mi><mi>e</mi><mo separator="true">,</mo><mi>a</mi><mi>r</mi><mi>r</mi><mi>a</mi><mi>y</mi></mrow><annotation encoding="application/x-tex">handle , array </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">han</span><span class="mord mathnormal">d</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">rr</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span></span></span></span>fields , string <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi><mi>e</mi><mi>l</mi><mi>i</mi><mi>m</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>r</mi><mo>=</mo><mi mathvariant="normal">"</mi><mo separator="true">,</mo><mi mathvariant="normal">"</mi><mo separator="true">,</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi></mrow><annotation encoding="application/x-tex">delimiter = "," , string </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">imi</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord">"</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">"</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span></span></span></span>enclosure = '"' , string $escape_char = "\\" ) : int|false
Code language: PHP (php)
The following example uses the fputcsv()
function to write data to a CSV file:
`<?php
$data = [ ['Symbol', 'Company', 'Price'], ['GOOG', 'Google Inc.', '800'], ['AAPL', 'Apple Inc.', '500'], ['AMZN', 'Amazon.com Inc.', '250'], ['YHOO', 'Yahoo! Inc.', '250'], ['FB', 'Facebook, Inc.', '30'], ];
$filename = 'stock.csv';
// open csv file for writing f=fopen(f = fopen(f=fopen(filename, 'w');
if ($f === false) { die('Error opening the file ' . $filename); }
// write each row at a time to a file foreach ($data as $row) { fputcsv($f, $row); }
// close the file fclose($f);`Code language: HTML, XML (xml)
How it works.
- First, define an array that holds the stock data.
- Second, open the
stock.csv
file for writing using thefopen()
function with the'w'
mode. - Third, loop through the
$data
array and write each each element as a line to the CSV file. - Finally, close the file using the
fclose()
function.
Writing Unicode characters #
If you’re dealing with Unicode characters especially creating a CSV file for using Microsoft Excel, you need to change the file header using the fputs()
function after opening the file as follows:
`<?php f=fopen(f = fopen(f=fopen(filename, 'w');
if ($f === false) { die('Error opening the file ' . $filename); }
fputs($f, (chr(0xEF) . chr(0xBB) . chr(0xBF))); // support unicode
// writing to a CSV file //....`Code language: HTML, XML (xml)
Reading from a CSV file #
To read a CSV file, you use the fgetcsv()
function:
fgetcsv ( resource <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>r</mi><mi>e</mi><mi>a</mi><mi>m</mi><mo separator="true">,</mo><mi>i</mi><mi>n</mi><mi>t</mi></mrow><annotation encoding="application/x-tex">stream , int </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.854em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">re</span><span class="mord mathnormal">am</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">in</span><span class="mord mathnormal">t</span></span></span></span>length = 0 , string <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>e</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>a</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>=</mo><mi mathvariant="normal">"</mi><mo separator="true">,</mo><mi mathvariant="normal">"</mi><mo separator="true">,</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi></mrow><annotation encoding="application/x-tex">separator = "," , string </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8095em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">se</span><span class="mord mathnormal">p</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord">"</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">"</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span></span></span></span>enclosure = '"' , string $escape = "\\" ) : array
Code language: PHP (php)
The fgetcsv()
function reads a line of CSV data from the file pointer’s position and places it into an array; each line of the CSV file is an array element.
The function fgetcsv()
returns false
if there is an error occurred while reading the file or when the file pointer reaches the end-of-file.
The following example shows how to read the stock.csv
file created above:
`<?php
$filename = './stock.csv'; $data = [];
// open the file f=fopen(f = fopen(f=fopen(filename, 'r');
if ($f === false) { die('Cannot open the file ' . $filename); }
// read each line in CSV file at a time while (($row = fgetcsv($f)) !== false) { data[]=data[] = data[]=row; }
// close the file fclose($f);`Code language: HTML, XML (xml)
How the script works.
- First, open the
stock.csv
file for reading using thefopen()
function. - Second, read each line in the file through the file handle and place it into an array. We use the while loop to read the entire CSV file until the file pointer reached the end-of-file.
- Third, close the file and display the array.
Summary #
- Use the
fputcsv()
function to write a row to a CSV file. - Use the
fgetcsv()
function to read a row from a CSV file.
Did you find this tutorial useful?