0

I am trying to get the contents of the MySQL table to a CSV file. Everything works fine except that a blank row is being inserted before the heading label that I pass through Array.

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

?>

Not really sure where is the blank line coming from.

4
  • Comment out the error_reporting so you see that no error is making the first line blank. You should also check all included files, if they have not the empty lines around the <?php ?> tags. And get rid of ob_start(), what is this for? Commented Jan 5, 2016 at 19:10
  • If I dont add ob_start() it does not download me a csv file. I displays the data on the browser. Also tried removing error_reporting But still the same. Commented Jan 5, 2016 at 19:25
  • The reason you need ob_start() is because the script between ob_start() and "echo $output;" is printing some content (your empty line probably). And therfore you can't set headers. I'll bet it's in your included files (auto_logout, config, ftp_settings). Allow errors, remove ob_start, solve all warnings, profit. Commented Jan 5, 2016 at 19:33
  • Atlast found out.. Thanks a lot @Reloecc. There was a white space in config file. Commented Jan 5, 2016 at 20:01

1 Answer 1

1

Including files will often add unwanted whitespaces if not cared properly. Suppressing errors will make the things worse in every case.

The reason you need ob_start() is because the script between ob_start() and "echo $output;" is printing some content (your empty line probably). And therfore you can't set headers. I'll bet it's in your included files (auto_logout, config, ftp_settings).

Allow errors, remove ob_start, solve all warnings, profit.

And btw: Do not use mysql extension, use mysqli instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.