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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.