Jump to content

Saving a variable as a TXT file on users pc?


spikypunker

Recommended Posts

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

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.