Question

I have to get mysql data in a CSV file on click of download button. The below code exports data successfully but adds space as separator. But I need comma to be separator not space, As there are space in mysql data and also on static header of csv file.

HTML code is below.

<form name="downloadform" id="downloadform" action="exportcsv.php" method="POST"> 
<div style="font-weight:bold; line-height:30px;" class="TableContainer">
        <label>Download CSV Format:</label>
        <button class="blue" name="btnDwld" id="btnDwld" type="submit">Download</button>
</div>
</form>

And export to csv code is below.

<?php 
require_once 'dbconfig.php';
if(isset($_POST['btnDwld']))
{
    $filname = "UploadRateWeight-".date("d-M-Y-H:i:s");
    header("Content-type: text/csv");
    header("Content-Disposition: attachment; filename=$filname.csv");
header("Pragma: no-cache");

    function echocsv($fields)
{
    $separator = ',';
        foreach ($fields as $field) {
           if (preg_match('/\\r|\\n|,|"/', $field)) {
                   $field = '"' . str_replace('"', '""', $field) . '"';
              } // end if
           echo $field.$separator;
        }   // end foreach
    echo "\r\n";
}

    $content = '';
    $title = '';
    $title = array('0'=>'head part1','1'=>'head part2','2'=>'head part3','3'=>'head part4','4'=>'head part5');
    $headerr = echocsv($title);

    $sql = "SELECT * FROM table_name";
    $query = mysql_query($sql);
    while($rs = mysql_fetch_array($query)) {
        $name = $rs["name"];
        $line = array('0'=>$name,'1'=>'','2'=>'','3'=>'','4'=>'');
        echocsv($line);
        //$content .= "\n";
    }
}

The out put in a .CSV file looks like below.

head
----------
name1
name2
name3

As you can see I have put 1st colum name as 'head part1' but it will show like 'head' as its taking space as separator.

So second column name takes this.

part1,head
----------

and third column name takes this.

part2,head
----------

and so on. So how to use a comma to be separator while exporting CSV file in php ??

Was it helpful?

Solution

Don't write csv file's manually. Use the built in function.

e.g. http://uk3.php.net/manual/en/function.fputcsv.php

<?php

$delimiter = ',';
$enclosure = '"';

$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields, $delimiter, $enclosure);
}

fclose($fp);
?>

The above code will create a file called file.csv with your data on it. If you then want to send this file to the user as a CSV file, you can do this:

<?php

// Send the generated csv file to the browser as a download
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
readfile('file.csv');

?>

Alternatively, you can send the CSV file to download directly without creating the file on server like this:

<?php

// mini-config
$delimiter = ',';

// Your data
$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

// Send headers
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");

// Output csv data
foreach ($list as $row) {
    echo implode($delimiter, $row) . "\r\n";
}

?>

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top