Jump to content

fputcsv stopped working with linux update


charlie321

Recommended Posts

We had to update our Linux server from php 5.4 to php 7.4.  Something has changed and I have a feeling it is due to changes in fputcsv.

When running the code below, instead of opening a csv file in libre calc, it fills the browser with the result.  Is there a way of fixing this so I get the csv file again?

<code>date_default_timezone_set("America/New_York");
$filename = "import.csv";
$fp = fopen('php://output', 'w');
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
$then = time() - 3601;
$query = "SELECT * FROM ppb_listings where user_id = '4' and unix_timestamp(start_time) > $then";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_row($result)) {
    fputcsv($fp, $row);
}
echo $result;
exit;
?>

</code>

Thanks for any help.

Link to comment
Share on other sites

Nothing changed in PHP to make that stop working, but your PHP setup might be different.

My guess what's wrong? header() only works if there hasn't been any output before it - or if you have a certain PHP setup which you really should not have. Showing the result in the browser means the Content-Disposition header was not sent, meaning there was some output. Probably a blank line or BOM.
Make sure the <?php is at the very beginning of the file and there is no output of any kind after it. Also make sure your editor is not saving the .php file with a UTF-8 BOM.

Link to comment
Share on other sites

The code I gave you before works with version 8, if you change the WHERE clause to what you really want instead of what you said you wanted...

WHERE insert_time > NOW() - INTERVAL 60 MINUTE
                           AND user_id = 4

Try it with  fclose($fp); instead of echo $result; 

It should give a dialogue box asking if you want to open or save.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.