{"id":9827,"date":"2021-04-16T10:00:06","date_gmt":"2021-04-16T01:00:06","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=9827"},"modified":"2021-04-15T19:27:26","modified_gmt":"2021-04-15T10:27:26","slug":"creating-downloadable-csv-file-in-php","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/9827","title":{"rendered":"Creating downloadable CSV file in PHP"},"content":{"rendered":"\n<p>This time I would like to share you how to automatically download CSV file using PHP. <\/p>\n\n\n\n<p>To download CSV file, the header() function and the php:\/\/output parameter must be used. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>header('Content-type: application\/csv');\nheader('Content-Disposition: attachment; filename=myCSV.csv');\nheader(\"Content-Transfer-Encoding: UTF-8\");<\/code><\/pre>\n\n\n\n<p>Setting the header is importance when downloading files automatically. In the first line of code, the <strong>content-type <\/strong>option is that HTTP header telling the browser that the MIME type of the content is application\/csv. <\/p>\n\n\n\n<p>Other particularly important option is in the second line of code, the <strong>filename <\/strong>option.  This is where we want to name the file that will be generated and downloaded. <\/p>\n\n\n\n<p> The other importance line of code is <strong>content transfer encoding<\/strong> ,this encoding method will transform  binary email message&nbsp;data into the US-ASCII plain text format.  <\/p>\n\n\n\n<p>For populating CSV, fopen() giving us access to PHP\u2019s output buffer. This line of code is that the file pointer connected to the output stream.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$f = fopen('php:\/\/output', 'a'); <\/code><\/pre>\n\n\n\n<p>Writing data to the Csv and we can define column headings like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fputcsv($f, array('Column 1', 'Column 2', 'Column 3'));<\/code><\/pre>\n\n\n\n<p>We can fetch data and  loop over the rows of data and output them. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql_connect('localhost', 'username', 'password');\nmysql_select_db('database');\n$rows = mysql_query('SELECT field1,field2,field3 FROM table');\n\nwhile ($row = mysql_fetch_assoc($rows)) fputcsv($f, $row);<\/code><\/pre>\n\n\n\n<p>By Ami<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9827\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/9827\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Creating downloadable CSV file in PHP\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9827\" data-text=\"Creating downloadable CSV file in PHP\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9827\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9827\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>This time I would like to share you how to automatically download CSV file using PHP. To download CSV file, th [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9827"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=9827"}],"version-history":[{"count":6,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9827\/revisions"}],"predecessor-version":[{"id":9834,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9827\/revisions\/9834"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=9827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=9827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=9827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}