present file to user in header

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
bluesman333
Forum Commoner
Posts: 52
Joined: Wed Dec 31, 2003 9:47 am

present file to user in header

Post by bluesman333 »

Can anyone tell me what is wrong here?

I want to create a .txt file and then give the user the option of opening it in excel or saving it somewhere.

Here is the code:

Code: Select all

$temp = tmpfile();
fwrite($temp, $out);//$out is a string containing the file output
	

header("Content-type: application/msexcel"); 
header("Content-Disposition: attachment; filename=".$eventname.".txt");
readfile($temp);
What I'm getting is a file with the html output.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

That is because your browser is trying to read a text file. save the file as a CSV file.
User avatar
bluesman333
Forum Commoner
Posts: 52
Joined: Wed Dec 31, 2003 9:47 am

Post by bluesman333 »

that didn't seem to help.

same result
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: present file to user in header

Post by Roja »

bluesman333 wrote:Can anyone tell me what is wrong here?

Code: Select all

$temp = tmpfile();
[b]header("Content-type: application/msexcel"); [/b]
header("Content-Disposition: attachment; filename=".$eventname.".txt");
readfile($temp);
What I'm getting is a file with the html output.
You meant to say..

Code: Select all

// Dont forget the dash!
header(&quote;Content-type: application/ms-excel&quote;);

header(&quote;Content-Disposition: attachment; filename=&quote;.$eventname.&quote;.xls&quote;);
Note the missing - in ms-excel, and the correct filename extension.
User avatar
bluesman333
Forum Commoner
Posts: 52
Joined: Wed Dec 31, 2003 9:47 am

Post by bluesman333 »

that doesn't work either.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Looking at your code, you're writing $out to the file, then sending the contents of that file. My guess is the contents of $out need to be adjusted if you're not getting what you want.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply