[wp-hackers] exporting a file in wp admin

Steve Taylor steve at sltaylor.co.uk
Sun May 23 12:30:30 UTC 2010


I'm trying to send a spreadsheet created with PHP on a WP admin page
to the browser for download. The relevant code I've got is:

header( "Content-Type: application/vnd.ms-excel" );
header( 'Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT' );
header( "Content-disposition: attachment; filename=download-logs.xls" );
header( 'Pragma: no-cache' );
echo $spreadsheet;
exit;

I gather there's a few different options for the header settings, but
I think the problem I've got is more basic than that. What I'm seeing
is just the normal WP admin page, with the spreadsheet data dumped as
a string. These are the response headers (via Web Developer in
Firefox):

Date: Sun, 23 May 2010 12:28:56 GMT
Server: Apache/2.2.3 (CentOS)
X-Powered-By: PHP/5.1.6
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Last-Modified: Sun, 23 May 2010 12:28:57 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
Keep-Alive: timeout=15, max=500
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
200 OK

I suspect I'm missing the trick of clearing the header settings
already specified by WP itself. I tried ob_clean() at the start, but
then I just see a blank WP admin page.

Any help appreciated!

Steve Taylor


More information about the wp-hackers mailing list