Php create csv file with header
I have data in a MySQL database. I am sending the user a URL to get their data out as a CSV file. I have the e-mailing of the link, MySQL query, etc. covered. How can I, when they click the link, have a pop-up to download a CVS with the record from MySQL? I have all the information to get the record already. I just don't see how to have PHP create the CSV file and let them download a file with a .csv extension.
d-_-b 20.4k39 gold badges139 silver badges233 bronze badges asked Oct 20, 2008 at 3:11
php://output
ntzm 4,6032 gold badges30 silver badges35 bronze badges answered Jun 27, 2011 at 13:43
10
Try:
etc Edit: Here's a snippet of code I use to optionally encode CSV fields:
Popnoodles 28.6k2 gold badges44 silver badges53 bronze badges answered Oct 20, 2008 at 3:15
Oleg BarshayOleg Barshay 3,7983 gold badges20 silver badges20 bronze badges 4 Here is an improved version of the function from php.net that @Andrew posted.
It is really easy to use and works great with MySQL(i)/PDO result sets.
Remember to
ntzm 4,6032 gold badges30 silver badges35 bronze badges answered Feb 14, 2012 at 19:07
XeoncrossXeoncross 54k77 gold badges255 silver badges361 bronze badges 6 In addition to all already said, you might need to add:
It's very useful when handling files with multiple languages in them, like people's names, or cities. answered Mar 14, 2013 at 23:32
StanStan 1,2511 gold badge15 silver badges24 bronze badges The thread is a little old, I know, but for future reference and for noobs as myself: Everyone else here explain how to create the CSV, but miss a basic part of the question: how to link. In order to link to download of the CSV-file, you just link to the .php-file, which in turn responds as being a .csv-file. The PHP headers do that. This enables cool stuff, like adding variables to the querystring and customize the output:
my_csv_creator.php can work with the variables given in the querystring and for example use different or customized database queries, change the columns of the CSV, personalize the filename and so on, e.g.:
answered Dec 10, 2011 at 9:07
LBJLBJ 1011 silver badge3 bronze badges 3 Create your file then return a reference to it with the correct header to trigger the Save As - edit the following as needed. Put your CSV data into $csvdata.
ntzm 4,6032 gold badges30 silver badges35 bronze badges answered Oct 20, 2008 at 3:16
typemismatchtypemismatch 2,0272 gold badges23 silver badges25 bronze badges 1 Here is a full working example using PDO and including column headers:
ntzm 4,6032 gold badges30 silver badges35 bronze badges answered Jul 1, 2013 at 18:05
JustinJustin 25.2k16 gold badges108 silver badges126 bronze badges First make data as a String with comma as the delimiter (separated with ","). Something like this
Hey It works very well....!!!! Thanks Peter Mortensen and Connor Burton
?> answered Feb 14, 2013 at 13:41
KaddyKaddy 311 bronze badge 1 You can simply write your data into CSV using fputcsv function. let us have a look at the example below. Write the list array to CSV file
ntzm 4,6032 gold badges30 silver badges35 bronze badges answered Mar 6, 2015 at 7:53
ShahbazShahbaz 3,3431 gold badge25 silver badges42 bronze badges Simple method -
So each line of the
ntzm 4,6032 gold badges30 silver badges35 bronze badges answered Feb 12, 2010 at 9:39
To have it send it as a CSV and have it give the file name, use header(): http://us2.php.net/header
As far as making the CSV itself, you would just loop through the result set, formatting the output and sending it, just like you would any other content. answered Oct 20, 2008 at 3:15
Gavin M. RoyGavin M. Roy 4,3614 gold badges35 silver badges29 bronze badges The easiest way is to use a dedicated CSV class like this:
answered Jul 25, 2011 at 18:51
Instead of:
Use:
answered Oct 29, 2010 at 15:29
Already very good solution came. I'm just puting the total code so that a newbie get total help
I have saved this code in csv-download.php Now see how I have used this data to download csv file
So when I have clicked the link it download the file without taking me to csv-download.php page on browser. answered Dec 17, 2012 at 16:19
zahid9izahid9i 5761 gold badge8 silver badges16 bronze badges Writing your own CSV code is probably a waste of your time, just use a package such as league/csv - it deals with all the difficult stuff for you, the documentation is good and it's very stable / reliable: http://csv.thephpleague.com/ You'll need to be using composer. If you don't know what composer is I highly recommend you have a look: https://getcomposer.org/ answered Jun 6, 2017 at 8:15
John HuntJohn Hunt 4,1088 gold badges42 silver badges59 bronze badges
How to write in CSV file using PHP script? Actually I was also searching for that too. It is kind of easy task with PHP. fputs(handler, content) - this function works efficiently for me. First you need to open the file in which you need to write content using fopen($CSVFileName, ‘wb’).
ntzm 4,6032 gold badges30 silver badges35 bronze badges answered Oct 22, 2012 at 2:40 2 public function actionExportnotificationresponselogdata() {
answered Mar 4 at 5:58
Put in the
David G 92k40 gold badges162 silver badges249 bronze badges answered Nov 18, 2010 at 10:57
|