_OwNeD.YoU_ Posted December 4, 2008 Share Posted December 4, 2008 Hi guys, I was wondering if there is any way to generate a file for users to download. I have text that a query grabs, i was wondering if there is anyway for it to be downloaded into a text file for download, but there are tons of them are would rather not have them actually stored on the server. Quote Link to comment Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 How's you php? and is the data stored on a database; You can use a script to force download using headers and build a txt file that will never exist on your server Quote Link to comment Share on other sites More sharing options...
s0c0 Posted December 4, 2008 Share Posted December 4, 2008 Here: function download($file) { if(file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); } } Quote Link to comment Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 That code is checking for a file that already exists. Do you want to use existing files or content from a database for example? Quote Link to comment Share on other sites More sharing options...
s0c0 Posted December 5, 2008 Share Posted December 5, 2008 That code is checking for a file that already exists. Do you want to use existing files or content from a database for example? Yes it checks first to see if the file exists and if it does, it then forces a download.... Quote Link to comment Share on other sites More sharing options...
_OwNeD.YoU_ Posted December 8, 2008 Author Share Posted December 8, 2008 yeaa its all stored in the database, having trouble get that to work though, <?php $file = "Hello World"; function download($file) { if(file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); } } ?> thats what i just have for a test, $file is where the infomation is stored correct? thanks again guys sorry for the late responce i was on vacation. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.