Vasily47 Posted October 31, 2023 Share Posted October 31, 2023 Hello all, I used to do a lot of PHP work before retiring in 2018 but my skills are rusty. I've written a function to gather data into a table then output it as a CSV file. This works, but the data's displayed on the browser rather than prompting the user to download the fiile (see image testpage.jpg), I suspect my headers aren't right, or there's some other problem with the code that's keeping the headers from working properly (see phpcode.jpg). I've tried this on my Macbook Pro with Firefox 118.0.2, Safari 16.6, MS Edge 118.0.2088.76, and Chrome 118.0.5993.117, and it doesn't work on any of them. FYI, I checked the server, and the file isn't being saved there. Thanks in advance for any advice you can give me! Quote Link to comment https://forums.phpfreaks.com/topic/317400-headers-to-force-file-download-in-browser-not-working/ Share on other sites More sharing options...
Vasily47 Posted October 31, 2023 Author Share Posted October 31, 2023 I was in PHP Coding Help when I posted this, I have no idea how it ended up in MySQL Help. Quote Link to comment https://forums.phpfreaks.com/topic/317400-headers-to-force-file-download-in-browser-not-working/#findComment-1612619 Share on other sites More sharing options...
Barand Posted October 31, 2023 Share Posted October 31, 2023 Moved to PHP forum Quote Link to comment https://forums.phpfreaks.com/topic/317400-headers-to-force-file-download-in-browser-not-working/#findComment-1612620 Share on other sites More sharing options...
requinix Posted October 31, 2023 Share Posted October 31, 2023 Images are for memes. If you want to show code then please use the Code <> button and paste it into your post. $header1 = "Content-Type text/csv"; $header2 = "Content-Disposition: attachment; filename='" . $csvname . "';"; They're both malformed: the first one is missing a colon, and the second one is using single-quotes when they're supposed to be double-quotes (and it has an extra semicolon that doesn't belong). Quote Link to comment https://forums.phpfreaks.com/topic/317400-headers-to-force-file-download-in-browser-not-working/#findComment-1612629 Share on other sites More sharing options...
Vasily47 Posted November 14, 2023 Author Share Posted November 14, 2023 (edited) Thanks; that fixes the header errors ... but it's still dumping the data to the browser rather than prompt for downloading it as a file. I've added the code below, if no one sees anything I guess I'll just format it as a table on the page and let the admin print it. Note: the shortcodes are for Paid Memberships Pro, the membership plugin I'm using, and I've activated the fpassthru function for the application on the server. /* Purpose: Export member neighborhood data as a CSV file. Author: Vasily Ingogly Date: November 14, 2023 */ function sjotl_list_export_neighborhoods_shortcode( $attrs = null ) { $userlist = get_users(); // Get list of WP_user objects $delimiter = ","; $today = date("mdY"); $csvname = "neighborhooddata-" . $today . ".csv"; $csvheader = array('MEMBER', 'LOGIN', 'NEIGHBORHOOD', 'EMAIL'); $header1 = 'Content-Type: text/csv'; $header2 = 'Content-Disposition: attachment; filename="' . $csvname . '"'; $fp = fopen('php://memory', 'w'); // Hold data in memory $returnstring = ""; fputcsv($fp, $csvheader, $delimiter); $numusers = 0; foreach ($userlist as &$current_user) { // Get each user's data $numusers++; $neighborhood_shortcode = '[pmpro_member field="neighborhoods" user_id="' . $current_user->id . '"]'; $email_shortcode = '[pmpro_member field="user_email" user_id="' . $current_user->id . '"]'; $username = $current_user->display_name; // Member $usernicename = $current_user->user_nicename; // Login $userneighborhood = do_shortcode( $neighborhood_shortcode ); // Neighborhood $useremail = do_shortcode( $email_shortcode ); // Email $memberdata = array($username, $usernicename, $userneighborhood, $useremail); fputcsv($fp, $memberdata, $delimiter); unset($current_user); } if ($numusers != 0) { fseek($fp, 0); // Rewind file and add headers header("$header1"); header("$header2"); $nchars = fpassthru($fp); fclose($fp); } else { fclose($fp); $returnstring = '<br>No user data found.<br>'; } return $returnstring; } add_shortcode('sjotl_list_export_neighborhoods', 'sjotl_list_export_neighborhoods_shortcode'); Edited November 14, 2023 by Vasily47 Quote Link to comment https://forums.phpfreaks.com/topic/317400-headers-to-force-file-download-in-browser-not-working/#findComment-1612879 Share on other sites More sharing options...
requinix Posted November 14, 2023 Share Posted November 14, 2023 Seems fine. Check with your browser's developer tools about what the web page response is, and confirm that it's returning that Content-Disposition header. If not then something is getting in the way... And while you're there, see if the tools report any errors or warnings. Quote Link to comment https://forums.phpfreaks.com/topic/317400-headers-to-force-file-download-in-browser-not-working/#findComment-1612883 Share on other sites More sharing options...
kicken Posted November 15, 2023 Share Posted November 15, 2023 Seems like you're outputting both HTML content and your exported CSV in the same request. You cannot do that. Whatever request is being made to export the CSV data needs to only output that CSV data, no HTML content. Quote Link to comment https://forums.phpfreaks.com/topic/317400-headers-to-force-file-download-in-browser-not-working/#findComment-1612884 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.