Allow users to download something as text file

Hey guys,

I am not sure where to look for info about something like this, even though it’s probably common. What I am trying to do is this: Allow a user to click a button that let’s him or her download a text file containing data from a MySQL database.

I know how to pull data from a database with PHP obviously, but I’m not sure how to allow it be downloadable in text file form to their computer.

I want the users to see this type of dialog when they click the button (but as a text file, not exe obviously):

If anyone can help me out here, maybe an explanation or link on where to begin. Thank you very much.

There is but for security and processing reasons its better to use a cron job as you can never 100% tell if a file has finished downloading and the script can end suddenly causing an error.

Its a lot more simple just to use a cron job hidden from the user view.

What you are talking about is forced header downloads, here is a link to a good tutorial but you can also find heaps more on Google [url=http://www.google.com/search?q=PHP+header+downloads&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a]PHP header downloads - Google Search

Thank you!!

Ah. Okay so heres a question. Do I actually have to create the text file itself and store it on my server in order for the user to be able to download it like this? Because that would require me to store perhaps thousands of text files (what they download can be different the next second, it’s dynamic). I know I can just remove the file right afterwards, but is there any simpler way?

Yes the file needs to exist on the server before readfile can work correctly, what you can do is use a cron job so that every 10 minutes or so it deletes files created with a time stamp of more then 10 minutes earlier. So for example

File time stamp: 1309119900
Server time stamp: 1309121700

For example what i would do is store the time stamp in the file name and run that against the current server time and if its greater then 10 minutes remove it from the server that way users currently downloading files won’t be affected by the cron job.

Great! One last question. Is there any way possible that you can use the same active PHP script that creates the file and offers it to the user, to delete the file about a minute later? Instead of a cron job?

Thanks so much for your answers

Thanks SL! I appreciate it very much.

No problem

That’s not exactly his question, nor is it entirely accurate. If his data is in a database he doesn’t NEED to make an external file for the download to happen.


<?php
	header('Cache-Control: public');
	header('Content-Description: File Transfer');
	header('Content-Disposition: attachment; filename=demo.txt');
	header('Content-Type: text/plain');
	header('Content-Transfer-Encoding: binary');
	echo 'This will create an auto-download .txt file!';
?>

ANY echo/print output will behave as the download content, not just readfile. Trust me, I do it with GD all the time with the imagegif, imagepng and imagejpg functions. (setting the content-type with image_type_to_mime_type)

I’d also suggest setting your character encoding heading while at it :smiley:

Thanks deathshadow60, i wasn’t trying to mislead [URL=“http://www.sitepoint.com/forums/member.php?u=408091”]svcghost in any way. I don’t use headers at all expect on the rare occasion for redirects so i was just going off what i read in tutorials.

Thanks for the info bud :slight_smile:

deathshadow, this is perfect, thank you so much!

And sgtlegend thank you too. I learned something interesting from your responses and will be applying something useful to my site w/ it.

:slight_smile: