_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. Link to comment https://forums.phpfreaks.com/topic/135534-generating-txt-file-for-download/ 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 Link to comment https://forums.phpfreaks.com/topic/135534-generating-txt-file-for-download/#findComment-706040 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); } } Link to comment https://forums.phpfreaks.com/topic/135534-generating-txt-file-for-download/#findComment-706041 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? Link to comment https://forums.phpfreaks.com/topic/135534-generating-txt-file-for-download/#findComment-706045 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.... Link to comment https://forums.phpfreaks.com/topic/135534-generating-txt-file-for-download/#findComment-706529 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. Link to comment https://forums.phpfreaks.com/topic/135534-generating-txt-file-for-download/#findComment-709389 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.