【问题标题】:A blank line is inserted at the beginning when Creating CSV using PHP MySQL使用 PHP MySQL 创建 CSV 时在开头插入一个空行
【发布时间】:2016-04-09 17:01:27
【问题描述】:

我正在尝试将 MySQL 表的内容转换为 CSV 文件。一切正常,只是在我通过数组的标题标签之前插入了一个空白行。

 <?php
    error_reporting(0);
    ob_start();
    session_start();
    include 'auto_logout.php';
    //echo $_SESSION['fullname'];
    if ((!isset($_SESSION['ufullname'])) && (!isset($_SESSION['fullname']))) {
        /* Redirect browser */
        header("Location: index.php");
        /* Make sure that code below does not get executed when we redirect. */
        exit();
    }
    $output = "";
    require_once('config/config.php');
    require_once("includes/ftp_settings.php");
    $table = "Download CSV"; // Enter Your Table Name 
    if (is_array($new_exist) && (is_array($ftp1))) {
        $ressd_new = 'select filename from files where uploaded_by IN ("' . implode('","', $new_exist) . '")';
        $resd_new  = mysql_query($ressd_new);
        while ($kkk_new = mysql_fetch_array($resd_new)) {
            $gotit_new = $kkk_new[0];
        }

        $resultka_new = $ftp1;

        $sql = 'SELECT slno, filename, uploaded_by, dateadded, timeadded, size FROM files where uploaded_by IN ("' . implode('","', $new_exist) . '") and filename IN ("' . implode('","', $resultka_new) . '") and size != "0"';

    } else {

        $sql = "SELECT slno, filename, uploaded_by, dateadded, timeadded, size FROM files where ftp_file  = '$ftp_server' and ftp_u_file = '$ftp_user_name' and ftp_p_file = '$ftp_user_pass' and size != '0'";

    }
    $sql           = mysql_query($sql);
    $columns_total = mysql_num_fields($sql);

    // Get The Field Name
    $heading1 = array(
        "Sl. No.",
        "File Name",
        "Uploaded By",
        "Date Added",
        "Time Added",
        "Size"
    );
    $inc = 0;
    for ($i = 0; $i < $columns_total; $i++) {
        $heading = $heading1[$inc];
        $output .= '"' . $heading . '",';
        $inc = $inc + 1;
    }
    $output .= "\n";

    // Get Records from the table

    while ($row = mysql_fetch_array($sql)) {
        for ($i = 0; $i < $columns_total; $i++) {
            $output .= '"' . $row["$i"] . '",';
        }
        $output .= "\n";
    }

    // Download the file

    $filename = "myFile.csv";
    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename=' . $filename);

    echo $output;
    exit;

?>

不确定空白行是从哪里来的。

【问题讨论】:

  • 注释掉error_reporting,这样您就可以看到没有错误使第一行变为空白。您还应该检查所有包含的文件,如果它们在 标记周围没有空行。并且去掉ob_start(),这是干什么用的?
  • 如果我不添加 ob_start() 它不会下载我的 csv 文件。我在浏览器上显示数据。也试过删除error_reporting 但还是一样。
  • 之所以需要 ob_start() 是因为 ob_start() 和 "echo $output;" 之间的脚本正在打印一些内容(可能是你的空行)。因此你不能设置标题。我敢打赌它在您包含的文件中(auto_logout、config、ftp_settings)。允许错误,移除 ob_start,解决所有警告,获利。
  • Atlast 发现.. 非常感谢@Reloecc。配置文件中有一个空格。

标签: php mysql csv


【解决方案1】:

如果处理不当,包含文件通常会添加不需要的空格。 在任何情况下,抑制错误都会使事情变得更糟。

你需要 ob_start() 的原因是 ob_start() 和 "echo $output;" 之间的脚本正在打印一些内容(可能是你的空行)。因此你不能设置标题。我敢打赌它在你包含的文件中(auto_logout、config、ftp_settings)。

允许错误,移除ob_start,解决所有警告,获利。

顺便说一句:不要使用 mysql 扩展,而是使用 mysqli。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 2017-07-27
    • 1970-01-01
    相关资源
    最近更新 更多