0

I've a issue while generating CSV file using php (fputcsv function). CSV File data is fine but interms of formatting generated file has 2 blank lines at the top of the file. Not sure where it is coming from. Can someone help with this?

var_dump($header); gives me below. here 'x','x' is two blank lines at the top.

x
x
    array(16) {
      [0]=>
      string(14) "DataProviderID"
      [1]=>
      string(12) "DataProvider"
      [2]=>
      string(8) "FamilyID"
      [3]=>
      string(10) "FamilyName"
      [4]=>
      string(10) "SecurityID"
      [5]=>
      string(4) "Name"
      [6]=>
      string(10) "PrimaryRic"
      [7]=>
      string(13) "Administrator"
      [8]=>
      string(16) "IsAdminEULocated"
      [9]=>
      string(21) "IsAdminOnEsmaRegister"
      [10]=>
      string(25) "IsBenchmarkOnEsmaRegister"
      [11]=>
      string(26) "IsBenchmarkOnAdminRegister"
      [12]=>
      string(23) "HasEUListedFundTracking"
      [13]=>
      string(25) "HasEUListedFutureOrOption"
      [14]=>
      string(20) "IsAdminPre2016Active"
      [15]=>
      string(24) "IsBenchmarkPre2018Active"
    }
    DataProviderID  DataProvider    FamilyID    FamilyName  SecurityID  Name    PrimaryRic  Administrator   IsAdminEULocated    IsAdminOnEsmaRegister   IsBenchmarkOnEsmaRegister   IsBenchmarkOnAdminRegister  HasEUListedFundTracking HasEUListedFutureOrOption   IsAdminPre2016Active    IsBenchmarkPre2018Active
    2   MSCI    36  MSCI Main Indices - Americas    17964   THE WORLD INDEX SMALL CAP-106230    .dMIWO000S0PUS  MSCI Limited    1   1   0   99  99  99  99  0
    2   MSCI    16730   MSCI Sector Indices - Europe Series 2   17996   EUROPE/OFFICE ELECTRONICS-106978    .dMIEU0OE00PUS  MSCI Limited    1   1   0   99  99  99  99  0

My code is as below:

<?php 
require_once '../upload/functions.php'; 

$connectionInfo = getdb();
#$connectionInfo = sqlsrv_connect(DB_HOST, $connectionInfo);

if ($connectionInfo === false ) {
    die (print_r(sqlsrv_errors(), true));
}

$sql = "SELECT * FROM <tablename>;";
$result = sqlsrv_query($connectionInfo, $sql);

if (!$result) die ('Couldn\'t fetch records');

$headers = array();

foreach (sqlsrv_field_metadata($result) as $fieldMetadata) {
    $headers[] = $fieldMetadata['Name'];
}

#var_dump($headers);

$fp = fopen('php://output', 'w');
if ($fp && $result) {
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="KYBAttributes.csv"');
    header('Pragma: no-cache');
    header('Expires: 0');
   fputcsv($fp, array_values($headers),"\t");
    while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
        $row = str_replace('"', '', $row);
        fputs($fp, implode("\t", $row)."\n");       
    }
    die;
}
?>

Please note that closing php tag '?>' is not the issue here.

Zulfiqar Dholkawala
  • 458
  • 2
  • 5
  • 19
  • 5
    the extra output is not in the code you show. make sure ../upload/functions.php does not contain whitespace after the PHP closing tag. – Gordon May 22 '18 at 09:11
  • As suggested by @Gordon I removed any white spaces in included by and also, removed closing '?>' php tag that fixed the issue. Thanks for the guidance..really appreciate that. – Zulfiqar Dholkawala May 22 '18 at 09:21

1 Answers1

2

Your source files contain the Personal Home-Page closing tag, ?>, followed by a newline. PHP is required to reproduce all lines not included within <? tags exactly. Therefore, any source file with unwanted newlines will cause newlines in the output, because that's what you've requested by including them.

user234461
  • 1,133
  • 12
  • 29