1
votes

I'm trying to write a csv file from an array, as a header of a csv file

$csv_fields[] = 'produto_quest';
$csv_fields[] = 'produto_quest1';
$csv_fields[] = 'comentario_aspecto_atm';
$csv_fields[] = 'aspecto_geral_int';
$csv_fields[] = 'organizacao_espaco';

$f = fopen('php://memory', 'w+');
fputcsv($f, $csv_fields, ";");

foreach ($relat as $fields) { // load from MySQL as a multidimensional array
    foreach($fields as $key => &$value1) {
        $value1 = iconv("UTF-8", "", $value1);
    }
    fputcsv($f, $fields, ";");
}
fseek($f, 0);
fpassthru($f);
fclose($f);

All the file is correct except a hidden character at the beginning of the file. If I open the file with notepad it display correctly, but in Excel there is a blank line at the beginning. Can anyone help me?

3
In the question title you say blank line, but then you say hidden character. What is it?cen
I can't identify, because I can't see what makes thhe first line of csv being blank, as in notepad nothing seems to be incorretRBrazao

3 Answers

4
votes

It looks fine to me. I tested with

$relat = array(range(1,5), range(3,7));

and I got no blank like or hidden character:

HTTP/1.1 200 OK
Date: Sat, 23 Nov 2013 23:26:27 GMT
Server: Apache/2.4.6 (Ubuntu)
X-Powered-By: PHP/5.5.3-1ubuntu2
Vary: Accept-Encoding
Content-Length: 109
Content-Type: text/html

produto_quest;produto_quest1;comentario_aspecto_atm;aspecto_geral_int;organizacao_espaco
1;2;3;4;5
3;4;5;6;7

Update: Since this is happening only to you and not the others, I believe this is because you have some newline character in your php source file. Make sure there is nothing outside the marks, like a newline at the beginning of the file before

You can test if this is the cause of the issue by calling:

ob_end_clean();

right before

fpassthru($f);
4
votes

Use ob_clean(); right before $f = fopen('php://memory', 'w+');

Example:

ob_clean();
$f = fopen('php://memory', 'w+');

It works fine for me to remove all blank lines at the begining of the CSV file.

0
votes

Similar question has been been discussed here:

How can I output a UTF-8 CSV in PHP that Excel will read properly?

Look at the most upvoted solution which echoes out the result instead of writing to temporary file. It goes like this:

header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=Customers_Export.csv');
echo "\xEF\xBB\xBF"; // UTF-8 BOM
echo $csv_file_content;

UPDATE:

header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=Customers_Export.csv');
echo "\xEF\xBB\xBF"; // UTF-8 BOM

$csv_fields[] = 'produto_quest';
$csv_fields[] = 'produto_quest1';
$csv_fields[] = 'comentario_aspecto_atm';
$csv_fields[] = 'aspecto_geral_int';
$csv_fields[] = 'organizacao_espaco';

$f = fopen('php://memory', 'w+');
fputcsv($f, $csv_fields, ";");

foreach ($relat as $fields) { // load from MySQL as a multidimensional array
    foreach($fields as $key => &$value1) {
        $value1 = iconv("UTF-8", "", $value1);
    }
    fputcsv($f, $fields, ";");
}
fseek($f, 0);
fpassthru($f);
fclose($f);