Forum Moderators: coopster

Message Too Old, No Replies

Create a csv file on-the-fly for downloading

How's it done most easily?

         

louponne

7:51 pm on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a large database-created table on a page, that needs to have a "download as csv file" link (actually, as xcel, but I'll bet that's much harder to create).

What's the simplest way to do that? Do I actually delete-and-recreate a csv file on the server every time?

jim_w

7:58 pm on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Somewhere on M$, support.microsoft.com, in the knowledge database, use their search, there is an example in VB to create a cvs file. It should be easy enough to convert that code to PERL or PHP, or whatever. AXS Visitor Tracking System script, available for free, does create a cvs file. AXS outputs the cvs file to your browser’s window so you can use the ‘Save As…’ to save it to your local hard drive. I don’t believe they create a file at all. Just send the results out the browser.

louponne

8:08 pm on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, jim_w, for that lightening-fast answer! But I'm just pretty much learning PHP, and really couldn't handle digging into VB or perl at the same time!

Timotheos

8:39 pm on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's a way to force the browser to download the page you've created by sending the right headers. Here's an example of how I can have someone download an excel/csv file.

header("Content-type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=mydata.csv");
header("Pragma: no-cache");
header("Expires: 0");

Now just have the rest of the php print out your variables in rows separated by commas and you're ready to go.

louponne

9:37 pm on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sounds great - but I'm not sure I'm understanding everything here. Are you saying that on my html page, I have a link pointing to makethecsv.php and then in makethescv.php, I put the code you're suggesting?

louponne

9:52 pm on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[edited incredibly stupid comment]

Many thanks! :)

Timotheos

10:00 pm on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cool ya got it. See it wasn't that hard.

The thing I do is inject the date.
header("Content-disposition: attachment; filename=mydata" . date("Y-m-d") . ".csv");

You could stick in your own variable..
header("Content-disposition: attachment; filename=" . $myfilename . ".csv");

Otherwise just let the user decide. At least on Windows the user is left with the option to save it to the place and filename that they choose.

louponne

7:15 am on Dec 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right, I'll definitely be determining the file name dynamically, *and* including the date.

Thanks again! :)

louponne

7:30 pm on Dec 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I hit a snag:

If there are commas, or line-breaks in any of the fields, then of course the csv file becomes a mess. I have replaced the commas with dashes, and that works for now, and I'm trying to replace the line-breaks with periods, and I assume that I'll get that working, but is there some way to keep commas and/or line-breaks in the data?

Here's what I'm doing for each field where I might have a comma or a line-break:

$trans = array("\r\n" => ". ", ","=>"-");
$value=strtr($value,$trans)

hm, I'll bet there's no way to keep a line break in a csv - but how 'bout commas?

NickCoons

7:46 pm on Dec 10, 2003 (gmt 0)

10+ Year Member



Probably not line breaks, but string values are often surrounded in quotes, this way the field ends in a quote and then a comma. So each individual comma in the field won't end the field.

Timotheos

8:10 pm on Dec 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi louponne,

Sorry I neglected to mention it's best to put quotes around each of the fields.

Tim

louponne

8:16 pm on Dec 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hm - well, it was working fine without the quotes! except for the commas and line-breaks that is.
I just tried adding quotes back in around each field:

$trans = array("\r\n" => ". ");
$value='"'.strtr($value,$trans).'"';

And although my fields with commas didn't get broken, the commas disappear entirely, making for a real mess!

Is adding quotes a good idea for some other reason I should be worried about?

NickCoons

8:27 pm on Dec 10, 2003 (gmt 0)

10+ Year Member



None that I can think of, though as far as I know it's standard to put quotes around text fields in a CSV file. I'm sure you can put the quotes around a text field without making your commas disappear :-).

louponne

8:32 pm on Dec 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hey, something else just occured to me. I'm doing this thusly:

header("Content-type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=tracking_".$variable."_".date("d-m-y").".csv");
header("Pragma: no-cache");
header("Expires: 0");
// print out the cvs file column heads
echo "column,head,names\n";
// get that data!
$sql="SELECT var1,var2,var3 FROM table"
$result=mysql_query($sql);
while ($info = mysql_fetch_array($result)){
// print out the data, line by line.
echo $info['var1'].",".$info['var2'].",".$info['var3']."\n";
}

It's working fine, but I'm testing with only a few records. Is that the right order to do things, or should I build up my text first, then send the header and the text?

louponne

8:37 pm on Dec 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sure you can put the quotes around a text field without making your commas disappear

ok, ok, I got that one figured out!

Timotheos

8:40 pm on Dec 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is that the right order to do things, or should I build up my text first

No you got the right order. Headers always must come before you echo anything.

So it works now?

Remember to put quotes around each label in your first row as well.

louponne

8:53 pm on Dec 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remember to put quotes around each label in your first row as well.

Sorry to be thick-headed here, but I'm not sure why? It's working perfectly, at least for me, without the quotes. The file is opening up in excel very nicely.

NickCoons

8:57 pm on Dec 10, 2003 (gmt 0)

10+ Year Member



<Is that the right order to do things, or should I build up my text first, then send the header and the text?>

I think it will work either way.. you can either generate the list first, then echo everything; or you can echo it on-the-fly. The only concern I can think of with echoing it on-the-fly is if the user is waiting for output because the server is still compiling the list. But since this is over the web, it's most likely that your server will be able to keep up with the internet connection, so this shouldn't be a concern.

Timotheos

9:13 pm on Dec 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry to be thick-headed here, but I'm not sure why? It's working perfectly, at least for me, without the quotes. The file is opening up in excel very nicely.

You're not thick-headed... I'm just anal ;-) It's just standard CSV format. While Excel might be forgiving others applications may not. Kinda like html standards, eh?

louponne

9:26 pm on Dec 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



NickCoons, yes, it's on the web, and it's an admin function, so it won't be getting hit by users, and will probably only run a max of a dozen times a day. Sounds as if I'm fine with it working the way it is now then.

Timotheos, many thanks for explaining. I see your point, and yes, sounds like html standards :)
In this specific case, the file will *always* be opened in excel, but it's good to know this sort of thing for... next time!

Many thanks again to all! :))

stargeek

11:08 pm on Dec 10, 2003 (gmt 0)

10+ Year Member



If you are still having problems i have a small open source script on my site which does exactly this, stickymail me for the url if you want it.