problem with this code

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

Post Reply
dakkonz
Forum Commoner
Posts: 69
Joined: Sat Dec 27, 2003 2:55 am
Location: Asia

problem with this code

Post 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?
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
dakkonz
Forum Commoner
Posts: 69
Joined: Sat Dec 27, 2003 2:55 am
Location: Asia

Post 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...
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Yeah - you still need the CSV building part you had before (assuming that works), just try the different Headers.
Post Reply