Page 1 of 1

problem with this code

Posted: Wed May 12, 2004 9:04 pm
by dakkonz

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?

Posted: Wed May 12, 2004 9:06 pm
by launchcode
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.

Posted: Thu May 13, 2004 2:50 am
by dakkonz
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...

Posted: Thu May 13, 2004 5:54 am
by launchcode
Yeah - you still need the CSV building part you had before (assuming that works), just try the different Headers.