You are here: Re: need to create and make download link for csv « PHP Programming Language « IT news, forums, messages
Re: need to create and make download link for csv

Posted by Petr Vileta on 09/18/06 23:26

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\>here 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($query);
echo "Content-Type: application/csv\n",
"Content-Disposition: filename=\"csv_data.csv\"\n\n";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo $row["field1"], ';', $row["field2"], ';' ...."\n";
}
mysql_free_result($result);
mysql_close($link);
?>


--

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация