PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
dakkonz
Forum Commoner
Posts: 69 Joined: Sat Dec 27, 2003 2:55 am
Location: Asia
Post
by dakkonz » Wed May 12, 2004 9:04 pm
Code: Select all
<?php
ob_start();
require_once('../includes/mysql.inc');
$sql = "select username, userid, email, address from user";
$result = mysql_query($sql);
$delimiter = ",";
$quote = '"';
$csv = "";
while ($row = mysql_fetch_assoc($result)) {
$first = true;
foreach ($row as $field) {
if (!$first) $csv .= $delimiter;
$field = preg_replace("/"/", """, $field);
$csv .= $quote.$field.$quote;
$first = false;
}
$csv .= "\n";
}
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE 5.5")) {
$att = "";
} else {
$att = " attachment";
}
header('Content-Type: application/download; name="export_new.csv"');
header('Content-Disposition:'.$att.' filename="export_new.csv"');
header('Content-Transfer-Encoding: binary');
echo $csv;
mysql_close();
ob_flush();
?>
i keep getting export.php as the file to be downloaded instead of export_new.csv as stated in the header... can anyone help me?
launchcode
Forum Contributor
Posts: 401 Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:
Post
by launchcode » Wed May 12, 2004 9:06 pm
I use this (and it works fine for my clients sites)
Code: Select all
Header("Content-Type: application/x-octet-stream");
Header("Content-Disposition: attachment; filename=$filename");
Header("Content-Length: $filesize");
I don't use the ob_start or ob_flush functions either, you don't really need them.
dakkonz
Forum Commoner
Posts: 69 Joined: Sat Dec 27, 2003 2:55 am
Location: Asia
Post
by dakkonz » Thu May 13, 2004 2:50 am
i cant seem to get it.....i got the data needed... but it is not in a excel spread sheet....I get for each row of data from the database, the values are all in a cell of the excel spreadsheet...
launchcode
Forum Contributor
Posts: 401 Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:
Post
by launchcode » Thu May 13, 2004 5:54 am
Yeah - you still need the CSV building part you had before (assuming that works), just try the different Headers.