{"id":126,"date":"2021-03-08T00:25:06","date_gmt":"2021-03-08T00:25:06","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=126"},"modified":"2021-07-20T13:11:15","modified_gmt":"2021-07-20T13:11:15","slug":"php-csv","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-csv\/","title":{"rendered":"PHP CSV"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to deal with CSV files in PHP, including creating and reading CSV files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='a-quick-introduction-to-csv-files'>A quick introduction to CSV files <a href=\"#a-quick-introduction-to-csv-files\" class=\"anchor\" id=\"a-quick-introduction-to-csv-files\" title=\"Anchor for A quick introduction to CSV files\">#<\/a><\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>When you open a CSV file using a spreadsheet application, you&#8217;ll see that the file is nicely formatted like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"476\" height=\"245\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/04\/php-csv.png\" alt=\"\" class=\"wp-image-1247\" srcset=\"https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/04\/php-csv.png 476w, https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/04\/php-csv-300x154.png 300w\" sizes=\"auto, (max-width: 476px) 100vw, 476px\" \/><\/figure>\n\n\n\n<p>However, if you view the CSV file in a text editor, it looks like the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Symbol,Company,Price\nGOOG,\"Google Inc.\",800\nAAPL,\"Apple Inc.\",500\nAMZN,\"Amazon.com Inc.\",250\nYHOO,\"Yahoo! Inc.\",250\nFB,\"Facebook, Inc.\",30<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Typically, a CSV file uses a comma (<code>,<\/code>) to separate fields in a CSV file. If the field content also contains a comma(<code>,<\/code>), the CSV file surrounds that field with double quotes, e.g., &#8220;Facebook, Inc..&#8221; <\/p>\n\n\n\n<p>Besides using the comma (<code>,<\/code>) character, a CSV file may use other characters to separate fields such as semicolon (<code>;<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='writing-to-a-csv-file'>Writing to a CSV file <a href=\"#writing-to-a-csv-file\" class=\"anchor\" id=\"writing-to-a-csv-file\" title=\"Anchor for Writing to a CSV file\">#<\/a><\/h2>\n\n\n\n<p>To write a line to a CSV file, you use the&nbsp;<code>fputcsv()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">fputcsv ( resource $handle , <span class=\"hljs-keyword\">array<\/span> $fields , string $delimiter = <span class=\"hljs-string\">\",\"<\/span> , string $enclosure = <span class=\"hljs-string\">'\"'<\/span> , string $escape_char = <span class=\"hljs-string\">\"\\\\\"<\/span> ) : int|<span class=\"hljs-keyword\">false<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example uses the <code>fputcsv()<\/code> function to write data to a CSV file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$data = &#91;\n\t&#91;<span class=\"hljs-string\">'Symbol'<\/span>, <span class=\"hljs-string\">'Company'<\/span>, <span class=\"hljs-string\">'Price'<\/span>],\n\t&#91;<span class=\"hljs-string\">'GOOG'<\/span>, <span class=\"hljs-string\">'Google Inc.'<\/span>, <span class=\"hljs-string\">'800'<\/span>],\n\t&#91;<span class=\"hljs-string\">'AAPL'<\/span>, <span class=\"hljs-string\">'Apple Inc.'<\/span>, <span class=\"hljs-string\">'500'<\/span>],\n\t&#91;<span class=\"hljs-string\">'AMZN'<\/span>, <span class=\"hljs-string\">'Amazon.com Inc.'<\/span>, <span class=\"hljs-string\">'250'<\/span>],\n\t&#91;<span class=\"hljs-string\">'YHOO'<\/span>, <span class=\"hljs-string\">'Yahoo! Inc.'<\/span>, <span class=\"hljs-string\">'250'<\/span>],\n\t&#91;<span class=\"hljs-string\">'FB'<\/span>, <span class=\"hljs-string\">'Facebook, Inc.'<\/span>, <span class=\"hljs-string\">'30'<\/span>],\n];\n\n$filename = <span class=\"hljs-string\">'stock.csv'<\/span>;\n\n<span class=\"hljs-comment\">\/\/ open csv file for writing<\/span>\n$f = fopen($filename, <span class=\"hljs-string\">'w'<\/span>);\n\n<span class=\"hljs-keyword\">if<\/span> ($f === <span class=\"hljs-keyword\">false<\/span>) {\n\t<span class=\"hljs-keyword\">die<\/span>(<span class=\"hljs-string\">'Error opening the file '<\/span> . $filename);\n}\n\n<span class=\"hljs-comment\">\/\/ write each row at a time to a file<\/span>\n<span class=\"hljs-keyword\">foreach<\/span> ($data <span class=\"hljs-keyword\">as<\/span> $row) {\n\tfputcsv($f, $row);\n}\n\n<span class=\"hljs-comment\">\/\/ close the file<\/span>\nfclose($f);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, define an array that holds the stock data.<\/li><li>Second, open the <code>stock.csv<\/code> file for writing using the <code>fopen()<\/code> function with the <code>'w'<\/code> mode.<\/li><li>Third, loop through the <code>$data<\/code> array and write each each element as a line to the CSV file.<\/li><li>Finally, close the file using the <code>fclose()<\/code> function.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='writing-unicode-characters'>Writing Unicode characters <a href=\"#writing-unicode-characters\" class=\"anchor\" id=\"writing-unicode-characters\" title=\"Anchor for Writing Unicode characters\">#<\/a><\/h3>\n\n\n\n<p>If you&#8217;re dealing with Unicode characters especially creating a CSV file for using Microsoft Excel, you need to change the file header using the <code>fputs()<\/code> function after opening the file as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n$f = fopen($filename, <span class=\"hljs-string\">'w'<\/span>);\n\n<span class=\"hljs-keyword\">if<\/span> ($f === <span class=\"hljs-keyword\">false<\/span>) {\n\t<span class=\"hljs-keyword\">die<\/span>(<span class=\"hljs-string\">'Error opening the file '<\/span> . $filename);\n}\n\nfputs($f, (chr(<span class=\"hljs-number\">0xEF<\/span>) . chr(<span class=\"hljs-number\">0xBB<\/span>) . chr(<span class=\"hljs-number\">0xBF<\/span>))); <span class=\"hljs-comment\">\/\/ support unicode<\/span>\n\n<span class=\"hljs-comment\">\/\/ writing to a CSV file<\/span>\n<span class=\"hljs-comment\">\/\/....<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='reading-from-a-csv-file'>Reading from a CSV file <a href=\"#reading-from-a-csv-file\" class=\"anchor\" id=\"reading-from-a-csv-file\" title=\"Anchor for Reading from a CSV file\">#<\/a><\/h2>\n\n\n\n<p>To read a CSV file, you use the <code>fgetcsv()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">fgetcsv ( resource $stream , int $length = <span class=\"hljs-number\">0<\/span> , string $separator = <span class=\"hljs-string\">\",\"<\/span> , string $enclosure = <span class=\"hljs-string\">'\"'<\/span> , string $escape = <span class=\"hljs-string\">\"\\\\\"<\/span> ) : <span class=\"hljs-keyword\">array<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>fgetcsv()<\/code> function reads a line of CSV data from the file pointer&#8217;s position and places it into an array; each line of the CSV file is an array element.<\/p>\n\n\n\n<p>The function <code>fgetcsv()<\/code> returns <code>false<\/code> if there is an error occurred while reading the file or when the file pointer reaches the end-of-file.<\/p>\n\n\n\n<p>The following example shows how to read the <code>stock.csv<\/code> file created above:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$filename = <span class=\"hljs-string\">'.\/stock.csv'<\/span>;\n$data = &#91;];\n\n<span class=\"hljs-comment\">\/\/ open the file<\/span>\n$f = fopen($filename, <span class=\"hljs-string\">'r'<\/span>);\n\n<span class=\"hljs-keyword\">if<\/span> ($f === <span class=\"hljs-keyword\">false<\/span>) {\n\t<span class=\"hljs-keyword\">die<\/span>(<span class=\"hljs-string\">'Cannot open the file '<\/span> . $filename);\n}\n\n<span class=\"hljs-comment\">\/\/ read each line in CSV file at a time<\/span>\n<span class=\"hljs-keyword\">while<\/span> (($row = fgetcsv($f)) !== <span class=\"hljs-keyword\">false<\/span>) {\n\t$data&#91;] = $row;\n}\n\n<span class=\"hljs-comment\">\/\/ close the file<\/span>\nfclose($f);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How the script works.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, open the <code>stock.csv<\/code> file for reading using the <code>fopen()<\/code> function.<\/li><li>Second, read each line in the file through the file handle and place it into an array. We use the <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-while\/\">while loop<\/a> to read the entire CSV file until the file pointer reached the end-of-file.<\/li><li>Third, close the file and display the array.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>fputcsv()<\/code> function to write a row to a CSV file.<\/li><li>Use the <code>fgetcsv()<\/code> function to read a row from a CSV file.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"126\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-csv\/\"\n\t\t\t\tdata-post-title=\"PHP CSV\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"126\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-csv\/\"\n\t\t\t\tdata-post-title=\"PHP CSV\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to deal with CSV files in PHP, including creating and reading CSV files.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":155,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-126","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=126"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/126\/revisions"}],"predecessor-version":[{"id":2329,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/126\/revisions\/2329"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}