charlie321 Posted July 29, 2021 Share Posted July 29, 2021 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted July 29, 2021 Share Posted July 29, 2021 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 29, 2021 Share Posted July 29, 2021 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. Quote Link to comment Share on other sites More sharing options...
charlie321 Posted July 30, 2021 Author Share Posted July 30, 2021 fclose($fp); did it. Thanks very much! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.