Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP生成的CSV文件在文件顶部有两个空行_Php_Csv - Fatal编程技术网

PHP生成的CSV文件在文件顶部有两个空行

PHP生成的CSV文件在文件顶部有两个空行,php,csv,Php,Csv,我在使用php(fputcsv函数)生成CSV文件时遇到问题。CSV文件数据很好,但生成的文件格式中间在文件顶部有两个空行。不知道它是从哪里来的。有人能帮忙吗 变量转储($header);下面给我。这里,'x','x'是顶部的两个空白行 x x array(16) { [0]=> string(14) "DataProviderID" [1]=> string(12) "DataProvider" [2]=>

我在使用php(fputcsv函数)生成CSV文件时遇到问题。CSV文件数据很好,但生成的文件格式中间在文件顶部有两个空行。不知道它是从哪里来的。有人能帮忙吗

变量转储($header);下面给我。这里,'x','x'是顶部的两个空白行

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
我的代码如下:

<?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;
}
?>

您的源文件包含个人主页结束标记,
?>
,后跟一个换行符。PHP需要复制未包含在
中的所有行。您的源文件包含个人主页结束标记,
?>
,后跟换行符。PHP需要复制
中未包含的所有行,额外的输出不在显示的代码中。确保../upload/functions.php在php结束标记后不包含空格。根据@Gordon的建议,我删除了includeby中的所有空格,还删除了修复该问题的结束“?>”php标记。谢谢你的指导。真的很感激。额外的输出不在你展示的代码中。确保../upload/functions.php在php结束标记后不包含空格。根据@Gordon的建议,我删除了includeby中的所有空格,还删除了修复该问题的结束“?>”php标记。谢谢你的指导,非常感谢。