Download html textarea content as a file

acr

I am working on a project where the requirement is to download a textarea content as a .txt file on a wordpress site.

After searching previous questions, I got below code which marked as a correct answer.

<html>
<body>
<form action="#" method="post">
<textarea name="text" rows="20" cols="100"></textarea>
<input type="submit" value="submit">Download Text</input>
</form>

<?PHP

if(isset($_POST['submit']))
 {   

$text = $_POST['text'];
print ($text);

$filename = 'test.txt';
$string = $text;

$fp = fopen($filename, "w");
fwrite($fp, $string);
fclose($fp);

header('Content-disposition: attachment; filename=test.txt');
header('Content-type: application/txt');
readfile('test.txt');

 }

?>

</body>
</html>

But for me it is not creating a .txt file. please help me to identify the issue

RRikesh

Your code actually creates a file on the server and it does not get deleted afterwards. If the code failed, it is possible that you didn't have permission to write on your server. You can trim your code down to the following so as no file is generated in the process:

<?php
if(isset($_POST['text']))
 {
   header('Content-disposition: attachment; filename=test.txt');
   header('Content-type: application/txt');
   echo $_POST['text'];
   exit; //stop writing
 }
?>
<html>
<body>
<form action="" method="post">
<textarea name="text" rows="20" cols="100"></textarea>
<input type="submit" value="submit" name="submit">Download Text</input>
</form>

</body>
</html>

Firstly, you should output the file before printing anything on screen and secondly, you forgot to add name="submit" to your original code. I didn't use it here, but I included for you to get an idea.


In the WordPress Context: Add this to your functions.php or plugin file:

function feed_text_download(){
if(isset($_POST['text_to_download']))
 {
   header('Content-disposition: attachment; filename=test.txt');
   header('Content-type: application/txt');
   echo $_POST['text_to_download'];
   exit; //stop writing
 }
}
add_action('after_setup_theme', 'feed_text_download');

Add the form to one of your template files or in the HTML editor of your post:

<form action="" method="post">
  <textarea name="text_to_download" rows="20" cols="100"></textarea>
  <input type="submit" value="submit" name="submit">Download Text</input>
</form>

It should be good :)


EDIT: Add Filename

HTML:

<form action="" method="post">
  <label>Filename:<input type="text" name="filename" /></label>
  <label>Text:<textarea cols="100" name="text_to_download" rows="20"></textarea></label>
  <input type="submit" name="submit" value="Download Text" />
</form>

PHP:

function feed_text_download() {
  if ( isset( $_POST['text_to_download'] ) && isset( $_POST['filename'] ) ) {

    $filename = sanitize_file_name( $_POST['filename'] );

    header( 'Content-disposition: attachment; filename=' . $filename );
    header( 'Content-type: application/txt' );
    echo $_POST['text_to_download'];
    exit; //stop writing
  }
}

add_action( 'after_setup_theme', 'feed_text_download' );

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to put and download the content of a div in html into pdf file using JS?

From Dev

Download file with specific content type

From Dev

textarea in backbone and underscore html decoding the content

From Dev

Make html() include content typed into textarea

From Dev

textarea in backbone and underscore html decoding the content

From Dev

HTML TextArea Fit Content (On Page Load)

From Dev

set Content of a TinyMCE textarea via HTML

From Dev

Get HTML content from textarea with TinyMCE 4

From Dev

How to add html content of selected div into a textarea?

From Dev

Download current html file

From Dev

How to use wget to download just html file with out downloading flash content(images and css sheets) ?

From Dev

Download a Table content (string) from Html Page without the need of a physical file

From Java

How to create a file with content and download it with THYMELEAF

From Dev

How to download a file via a Chrome Content Script?

From Dev

How to download a content as a pdf file in android?

From Dev

How to download file without specific content type

From Dev

How to create a file with content and download it with THYMELEAF

From Dev

Content negotiation for File Download In Web API

From Dev

How to download a content as a pdf file in android?

From Dev

How to download a xml file with content from inputtextarea?

From Dev

Is there a way to download parts of the content of a zip file?

From Dev

How to assign structure to file content in download widget

From Dev

Download file from webserver and read content on android

From Dev

Download json file content jQuery node js

From Dev

Download a html file on click of span

From Dev

download file from server in html

From Dev

Download and encode HTML page into file

From Dev

Saving HTML5 textarea contents to file

From Dev

Export content inside textarea to Html not working on Internet Explorer 11

Related Related

  1. 1

    How to put and download the content of a div in html into pdf file using JS?

  2. 2

    Download file with specific content type

  3. 3

    textarea in backbone and underscore html decoding the content

  4. 4

    Make html() include content typed into textarea

  5. 5

    textarea in backbone and underscore html decoding the content

  6. 6

    HTML TextArea Fit Content (On Page Load)

  7. 7

    set Content of a TinyMCE textarea via HTML

  8. 8

    Get HTML content from textarea with TinyMCE 4

  9. 9

    How to add html content of selected div into a textarea?

  10. 10

    Download current html file

  11. 11

    How to use wget to download just html file with out downloading flash content(images and css sheets) ?

  12. 12

    Download a Table content (string) from Html Page without the need of a physical file

  13. 13

    How to create a file with content and download it with THYMELEAF

  14. 14

    How to download a file via a Chrome Content Script?

  15. 15

    How to download a content as a pdf file in android?

  16. 16

    How to download file without specific content type

  17. 17

    How to create a file with content and download it with THYMELEAF

  18. 18

    Content negotiation for File Download In Web API

  19. 19

    How to download a content as a pdf file in android?

  20. 20

    How to download a xml file with content from inputtextarea?

  21. 21

    Is there a way to download parts of the content of a zip file?

  22. 22

    How to assign structure to file content in download widget

  23. 23

    Download file from webserver and read content on android

  24. 24

    Download json file content jQuery node js

  25. 25

    Download a html file on click of span

  26. 26

    download file from server in html

  27. 27

    Download and encode HTML page into file

  28. 28

    Saving HTML5 textarea contents to file

  29. 29

    Export content inside textarea to Html not working on Internet Explorer 11

HotTag

Archive