473,887 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need to create and make download link for csv

Hello there,

i have an app that i need to be able to publish a link to download a
csv file.
i know that i can use php to make the file, but how to i link to it
through php.
like if i have an html file that has a link in it like this
<a href="somedata. csv">here be data</a>
or would i make that a .php file that would generate a csv ?

the data will come from MySQL.

any ideas?

thanks

Sep 18 '06 #1
5 5813
nephish wrote:
Hello there,

i have an app that i need to be able to publish a link to download a
csv file.
i know that i can use php to make the file, but how to i link to it
through php.
like if i have an html file that has a link in it like this
<a href="somedata. csv">here be data</a>
or would i make that a .php file that would generate a csv ?

the data will come from MySQL.

any ideas?
By me the best choice is to have page with links as php page and have other
php page for generating and sending csv file direct to browser.

download.php
-------------------
<html><body.. ..
<?php
# link for csv where mysql query contain eg. "... WHERE id=12345"
echo "<a href=\"sendcsv. php?id=12345\>h ere be data</a>\n";
?>
</body></html>

sendcsv.php
-----------------
<?php
$link = mysql_connect(" localhost", "user, "password") or die(mysql_error ());
mysql_select_db ("mydb") or die(mysql_error ());
$id = $_GET["id"]*1; # multiply by 1 preempt sql injection
$query = "SELECT * FROM mytable WHERE id=$id";
$result = mysql_query($qu ery);
echo "Content-Type: application/csv\n",
"Content-Disposition: filename=\"csv_ data.csv\"\n\n" ;
while ($row = mysql_fetch_arr ay($result, MYSQL_ASSOC))
{
echo $row["field1"], ';', $row["field2"], ';' ...."\n";
}
mysql_free_resu lt($result);
mysql_close($li nk);
?>
--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
Sep 18 '06 #2

Petr Vileta wrote:
nephish wrote:
Hello there,

i have an app that i need to be able to publish a link to download a
csv file.
i know that i can use php to make the file, but how to i link to it
through php.
like if i have an html file that has a link in it like this
<a href="somedata. csv">here be data</a>
or would i make that a .php file that would generate a csv ?

the data will come from MySQL.

any ideas?
By me the best choice is to have page with links as php page and have other
php page for generating and sending csv file direct to browser.

download.php
-------------------
<html><body.. ..
<?php
# link for csv where mysql query contain eg. "... WHERE id=12345"
echo "<a href=\"sendcsv. php?id=12345\>h ere be data</a>\n";
?>
</body></html>

sendcsv.php
-----------------
<?php
$link = mysql_connect(" localhost", "user, "password") or die(mysql_error ());
mysql_select_db ("mydb") or die(mysql_error ());
$id = $_GET["id"]*1; # multiply by 1 preempt sql injection
$query = "SELECT * FROM mytable WHERE id=$id";
$result = mysql_query($qu ery);
echo "Content-Type: application/csv\n",
"Content-Disposition: filename=\"csv_ data.csv\"\n\n" ;
while ($row = mysql_fetch_arr ay($result, MYSQL_ASSOC))
{
echo $row["field1"], ';', $row["field2"], ';' ...."\n";
}
mysql_free_resu lt($result);
mysql_close($li nk);
?>
--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
cool, thanks a lot !
-sk

Sep 19 '06 #3
PHP to generate the CSV is probably easiest:

<?php header("Content-Type", "text/csv");
// code spewing CSV here
?>

nephish wrote:
Hello there,

i have an app that i need to be able to publish a link to download a
csv file.
i know that i can use php to make the file, but how to i link to it
through php.
like if i have an html file that has a link in it like this
<a href="somedata. csv">here be data</a>
or would i make that a .php file that would generate a csv ?

the data will come from MySQL.

any ideas?

thanks
Sep 19 '06 #4

James McIninch wrote:
PHP to generate the CSV is probably easiest:

<?php header("Content-Type", "text/csv");
// code spewing CSV here
?>

nephish wrote:
Hello there,

i have an app that i need to be able to publish a link to download a
csv file.
i know that i can use php to make the file, but how to i link to it
through php.
like if i have an html file that has a link in it like this
<a href="somedata. csv">here be data</a>
or would i make that a .php file that would generate a csv ?

the data will come from MySQL.

any ideas?

thanks

i like the code spewing csv bit. But the filename will be a .php, right
? Will the browser the client uses recognize that it is producing a csv
for them to download? You know, with the little [save as] dialog?

thanks
shawn

Sep 19 '06 #5
James McIninch wrote:
PHP to generate the CSV is probably easiest:

<?php header("Content-Type", "text/csv");
// code spewing CSV here
>>
What is the right type "text/csv" or "applicatio n/csv" ? For me work both
but what is right?

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)

Sep 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2272
by: Peivasteh | last post by:
Hi Anybody can help who know PHP . I want you to have look at this site first. http://www.bigshotmedia.com How this site use PHP to protect swf files to not download to cashin temparary folder. Anybody knows about this how PHP works or what is PHP script. Thanks Behzad Peivasteh
8
4529
by: Ted Miller | last post by:
Hi folks, I'm looking at moving a large base of C++ code to .Net under tight time constraints. The code runs in mission-critical environments, and I am extremely concerned about the loader lock problem and the potential for deadlocks. After pouring over the available information, and trying a few experiments, I am still left with a few questions and issues I hope someone out there can shed some light on.
15
4663
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to communicate with a MySQL database table on a web server, from inside of my company's Access-VBA application. I know VBA pretty well but have never before needed to do this HTTP/XML/MySQL type functions.
1
1557
by: satan | last post by:
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList. These are my classes: public class UnorderedLinkedList extends LinkedListClass { public UnorderedLinkedList()
7
1883
by: satan | last post by:
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList. These are my classes: public class UnorderedLinkedList extends LinkedListClass { public UnorderedLinkedList()
46
2566
by: Bruce W. Darby | last post by:
This will be my very first VB.Net application and it's pretty simple. But I've got a snag in my syntax somewhere. Was hoping that someone could point me in the right direction. The history: My work involves creating custom packages of our software product for golf courses that purchase our software. The course data is kept as a back up in the event the course needs us to replace their custom files. Each course has a folder of it's own...
20
4335
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site is structured as an upside-down tree, and (if I remember correctly) never more than 4 levels. The site basically grew (like the creeping black blob) ... all the pages were created in Notepad over the last
13
2806
by: Neil Gould | last post by:
Hi all, Thanks for the suggestions for our club's initial bulkmail sending routines. I did read up on the use of ASP vs. a separate COM app such as ASPEmail, and determined that for our club's purposes the ASP code that loops through the data and sends individual messages vs. BCC performs adequately for simple messaging. The next phase is to email the club's newsletter, which is in PDF format, and I've run into the problem that CDO...
12
4035
by: Bob Bedford | last post by:
I've to build a website where the customer can buy an image. I'm thinking in the 2 ways to let people get those images: receive them by Email or send them an image link wich validity is limited in time. The second may help me to know if an image has been downloaded and how many times. How to build such download link ? The informations I'd like is the userid, the datetime limit and the imagefile, off course everything must be encrypted...
0
9957
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10877
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10434
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9593
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7143
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5810
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4633
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4239
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.