PHP - Streaming CSV Files to Browser
<p>I'm having issues getting CSV files to download directly to my browser without storing the files somewhere on my server. I found a tutorial (<a href="https://www.perpetual-beta.org/weblog/php-stream-file-direct.html" rel="nofollow noreferrer">https://www.perpetual-beta.org/weblog/php-stream-file-direct.html</a>) but I can't seem to get it working!</p>

<p>This is my main function: </p>

<pre><code>public function exportRecord($id) {
    $contest = (string) $this-&gt;getContest($id);
    $entries = (array) $this-&gt;getEntries($id);

    $filename = sprintf('%1$s-%2$s-%3$s', str_replace(' ', '', $contest['name']), date('Ymd'), date('His'));
    $output = fopen('php://output', 'w');

    ob_start();

    $header = array(
        'First Name',
        'Last Name',
        'Address',
        'City',
        'State',
        'Zip',
        'Phone',
        'Email',
        'Item Purchased',
        'Partner Name',
        'Partner #',
        'Date Entered',
        'Subscribe'
    );

    fputcsv($output, $header);

    foreach ($entries as $entry) {
        fputcsv($output, $entry);
    }

    $string = ob_get_clean();

    header('Pragma: public');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Cache-Control: private', false);
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . $filename . '.csv";');
    header('Content-Transfer-Encoding: binary');

    exit($string);
}
</code></pre>

<p>I am calling the following two functions, which just do queries on my database...</p>

<pre><code>private function getContest($id) {
    $query = sprintf(
        'SELECT name ' .
        'FROM contests ' .
        'WHERE id = %s;',
        $id
    );

    $result = $this-&gt;mysql-&gt;query($query);
    $response = '';

    if ($result) {
        while ($row = mysqli_fetch_assoc($result)) {
            $response = $row;
        }
    } else {
        echo $this-&gt;mysql-&gt;error;
    }

    return $response;
}

private function getEntries($id) {
    $query = sprintf(
        'SELECT firstName, ' .
            'lastName, ' .
            'CONCAT(address1, ", ", address2), ' .
            'city, ' .
            'state, ' .
            'zip, ' .
            'phone, ' .
            'email, ' .
            'itemPurchased, ' .
            'partnerName, ' .
            'partnerNum, ' .
            'dateEntered, ' .
            'subscribe ' .
        'FROM entries ' .
        'WHERE contestID = %s;',
        $id
    );

    $result = $this-&gt;mysql-&gt;query($query);
    $response = '';

    if ($result) {
        while ($row = mysqli_fetch_assoc($result)) {
            $response[] = $row;
        }
    } else {
        echo $this-&gt;mysql-&gt;error;
    }

    return $response;
}
</code></pre>

<p>I appreciate any help/advice that you'll give! Here is a screenshot, just to give you an idea of what the issue is: 
<a href="https://i.stack.imgur.com/uuFjq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uuFjq.png" alt="Screenshot"></a>
Thank you!</p>