3

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?

RBrazao
  • 49
  • 1
  • 6
  • In the question title you say blank line, but then you say hidden character. What is it? – cen Nov 23 '13 at 23:25
  • 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 incorret – RBrazao Nov 23 '13 at 23:43

3 Answers3

10

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.

Silambarasan R
  • 1,346
  • 15
  • 22
5

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);
Andrei B
  • 2,740
  • 17
  • 12
  • You are getting a blank line before the first character, as I also get – RBrazao Nov 23 '13 at 23:49
  • I obtain https://www.dropbox.com/s/ix20tp5xf17dldt/Capturar.PNG, even with the sugestion from @fiLLLipnet – RBrazao Nov 24 '13 at 00:08
  • if you're refering to the blank line above produto_quest, that's the standard blank line between HTTP header and body. All HTTP responses have that. After it, as you can see, there's no other blank line. – Andrei B Nov 24 '13 at 12:03
  • Please note that it doesn't fix the actual problem (then extra character outside – Andrei B Nov 24 '13 at 16:59
0

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);
Community
  • 1
  • 1
fiLLLipnet
  • 111
  • 1
  • 3
  • you are sugesting to echoe the file instead of using fputcsv? – RBrazao Nov 23 '13 at 23:39
  • I was just looking at the other suggestion, but I see that instead of the last line in the code I suggested, you can paste in your code. That should work :) I updated my suggestion. – fiLLLipnet Nov 23 '13 at 23:41
  • With this echo, I get "" at the begging or end of the file, depending on the echo position – RBrazao Nov 23 '13 at 23:47
  • If I use the updated code, it looks like this in Excel: https://db.tt/xy3QM237 And like this in Notepad++: https://db.tt/9RvUoq47 – fiLLLipnet Nov 23 '13 at 23:53
  • The BOM marker is supposed to be the first character of the file. If the update in my answer is correct, then your document has some whitespace character (from the php source), then the BOM, then the CSV data. This is why BOM shows up as regular characters. – Andrei B Nov 24 '13 at 12:24
  • If I paste my code, and only my code into a new php file, it works. What does your complete code look like? Does it output anything else before the pasted code? – fiLLLipnet Nov 24 '13 at 13:24