Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a CSV file 1

Status
Not open for further replies.

jimoblak

Instructor
Oct 23, 2001
3,620
US
Is it possible to create a CSV file in the browser so that when someone clicks on a link to get the file (a PHP page pulling from a MySQL database), the file will be sent as a plain comma delimited text file with a .CSV extension instead of a .PHP extension?

Creating the comma separated file is no problem. I would just like to know how to make the server send it with a .CSV extension so that it can be saved instantly on my users' computer for use later in MS Excel.
 
You didn't specify what web server you were on, so I'll assume Apache -- I have a solution for that server.

Use mod_rewrite.

Assume you have a directory called data_out. Add these two configuration directives to your virtual server configuration:

RewriteEngine on
RewriteRule ^/data_out/data.csv$ /data_out/csvout.php

Any time you attemp to pull up data.csv, Apache will run the PHP script csvout.php.

Make sure that csvout.php modifies the "Content-type" header to match whatever a .csv file is. I used "text/plain", but there may be a canonical content-type. ______________________________________________________________________
TANSTAAFL!
 
sleipnir214 you are (and have just recieved) a star.

I was mid writing the same thing for my work when you posted.

Easy innit, thats half a day off [lol] ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top