dousui7410 2015-10-27 02:37
浏览 175

PHP导出到CSV文件的前两行为空

I have the below data in an array and want to write to CSV file. Everything is working except i see first two empty lines in the .csv file

Any help is appreciated... Thanks again!!!!

Output in CSV file looks like empty Line1
empty Line2
ip address ip state description hostmane mac owner device notes data......

Array data:

Array (
[0] =>Array ( [ip address] => 1.3.2.1 [ip state] => Active [description] => [hostname] => [mac] => [owner] => [device] => [note] => ) 
[1] =>Array ( [ip address] => 1.3.2.2 [ip state] => Reserved [description] => [hostname] => linux [mac] => [owner] => test [device] => Linux Server [note] => Linux123 )
[2] => Array ( [ip address] => 1.3.2.3 [ip state] => Active [description] => [hostname] => [mac] => [owner] => [device] => [note] => )
[3] => Array ( [ip address] => 1.3.2.4 [ip state] => Active [description] => [hostname] => [mac] => [owner] => [device] => [note] => )
)  

PHP Function i am using to write to CSV file is:

switch("export-to-csv")
{   
    case "export-to-csv" :
        // Submission from
        $filename = $_GET["exformat"] . ".csv";
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-type: text/csv");
        header("Content-Disposition: attachment; filename=\"$filename\"");
        header("Expires: 0");
        ExportCSVFile($data);
        //$_GET["exformat"] = '';
        exit();
    default :
        die("Unknown action : ".$_POST["action"]);
        break;
}

function ExportCSVFile($records) {
    // create a file pointer connected to the output stream 
    #print_r ($records); return;
    $fh = fopen( 'php://output', 'w' );
    $heading = false;
    if(!empty($records)) {
        foreach($records as $row) {            
            if(!$heading)  {
              // output the column headings
              fputcsv($fh, array_keys($row));
              $heading = true;
            }
        // loop over the rows, outputting them
         fputcsv($fh, array_values($row));              
      }       

    }
}
  • 写回答

2条回答 默认 最新

  • dongzhong8691 2015-10-27 04:21
    关注

    Please any of the files like index.php of the framework or any other file having empty lines in the beginning, this can cause empty lines in the csv.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题