spikypunker Posted March 11, 2010 Share Posted March 11, 2010 Hi Guys I know this may be frowned upon? I can see potential for this kind of operation to be abused. But i have an online HTML editor and need the output to be saved as a TXT file on the users system. It would be best if a file browser appears and they choose save location etc etc. So far i'm just passing the content to a page called save.php which is picking it up, so where do i go from here? Kind regards, Chris Quote Link to comment https://forums.phpfreaks.com/topic/194891-saving-a-variable-as-a-txt-file-on-users-pc/ Share on other sites More sharing options...
Wolphie Posted March 11, 2010 Share Posted March 11, 2010 That seems like a legit reason. However, when you're doing this nothing else should be echo'd on the page where this code belongs. If you have all of the text you want to place in the file you can use this: <?php // Once the user visits this page, the code below will be ran and a file transfer will be initiated by the browser. // Name of the file $filename = 'filename.txt'; // Set the appropriate headers to tell the browser that it's a file to downlaod header('Content-Type: application/octet-stream'); header('Content-Description: File Transfer'); header('Content-Disposition: attachment; filename="'. $filename .'"'); header('Pragma: no-cache'); header('Expires: 0'); // $output contains the text that you want to place into the .txt file. echo $output; ?> Quote Link to comment https://forums.phpfreaks.com/topic/194891-saving-a-variable-as-a-txt-file-on-users-pc/#findComment-1024710 Share on other sites More sharing options...
spikypunker Posted March 11, 2010 Author Share Posted March 11, 2010 Heeey! That's great! Works perfectly Is there anyway of giving the user a choice as to where to save it to? That script is awesome, but it just gives the option to save, and then saves into your downloads. Is it fairly simple to add the extra functionality? If not then i'll be happy with the way it works now! Cheers again! peace Quote Link to comment https://forums.phpfreaks.com/topic/194891-saving-a-variable-as-a-txt-file-on-users-pc/#findComment-1024751 Share on other sites More sharing options...
spikypunker Posted March 11, 2010 Author Share Posted March 11, 2010 Quote Link to comment https://forums.phpfreaks.com/topic/194891-saving-a-variable-as-a-txt-file-on-users-pc/#findComment-1024781 Share on other sites More sharing options...
spikypunker Posted March 11, 2010 Author Share Posted March 11, 2010 :'( Quote Link to comment https://forums.phpfreaks.com/topic/194891-saving-a-variable-as-a-txt-file-on-users-pc/#findComment-1024830 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.