Jump to content

Recommended Posts

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!

testpage.jpg

phpcode.jpg

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).

  • 2 weeks later...

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 by Vasily47

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.

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.

 

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.