Download File Securely

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
Number1SuperGuy
Forum Newbie
Posts: 10
Joined: Thu Jun 17, 2010 11:21 am

Download File Securely

Post by Number1SuperGuy »

I need to create a TXT file for the user, but I don't want to simply store it the server because it's sensitive information, and it seems like someone could just type www.server.com/filename.TXT and steal this sensitive information.

Is it possible to create a TXT file for a user to download without ever storing it on the server? Any other ideas?
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Download File Securely

Post by Apollo »

Several options:

1 - generate the data on-the-fly whenever the user requests it
2 - encrypt the data so that downloading is only possible with the right credentials

And regardless of this, make sure you're using SSL (https) otherwise anyone will be able to sniff the data, no matter how well you protect it server-side.
Number1SuperGuy
Forum Newbie
Posts: 10
Joined: Thu Jun 17, 2010 11:21 am

Re: Download File Securely

Post by Number1SuperGuy »

Definitely getting SSL ;) I'm able to generate this data on-the-fly, in fact that's preferred, how can I let the user save this data as a text file without storing it on the server first?
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Download File Securely

Post by Apollo »

Code: Select all

<?php
$s = "Generate file contents here";
$s .= "\n bla \n bla \n bla...";
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: text/plain; charset=utf-8');
header('Content-Length: '.strlen($s));
header('Content-Disposition: attachment; filename="TopSecret.txt"');
die($s);
?>
Post Reply