1

I have my head around Jquery now but want to save a text area to a txt file. With the ultimate goal as being able to upload a text file to the same place as well. What am I missing in my code?

Not sure where I am define the textarea in this code or whether it should be in another file or if the html file should have the suffix of php?

Cheers below is my attempt at the php.

CODE

<?php
    if(isset($_POST['submit_save'])) {
        $file = "output.txt";
        $output = $_POST['output_str'];
        file_put_contents($file, $output);
        $text = file_get_contents($file);

        header("Content-type: application/text");
        header("Content-Disposition: attachment; filename=\"$file\"");
        echo $text; 
    }  
    else 
    {    
        $_POST['output_str'] = "";
    }
?>

</head>

<body>
    <input id="submit_save" type="button" value="save" />
    </br></br></br>
    <div id="opt"></div>
</body>
</html>
Veger
  • 37,240
  • 11
  • 105
  • 116
Ferg
  • 111
  • 1
  • 7

4 Answers4

2

$_POST is for name. You need to add name to #submit_save

<input name="submit_save" id="submit_save" type="button" value="save" />
Idrizi.A
  • 9,819
  • 11
  • 47
  • 88
0

Use the readfile function and don't forget to "exit" after to not corrupt the file. Like Enve said, POST array keys are html "name".

Here an example from the man pages :

<?php
$file = 'monkey.gif';

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');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}

Source : http://php.net/manual/en/function.readfile.php

Community
  • 1
  • 1
Fabien Sa
  • 9,135
  • 4
  • 37
  • 44
0

Your PHP script runs on your webserver, not in the browser. The result of the script is what the browser receives!

When reading your question(s), it is not clear (to me) whether you want to send the entered data (of the text area) to the browser as a file or as a HTML page. You seem to want both, which is not possible, choose one.

Fab Sa already demonstrated how to send a file to the browser from PHP, so I will not discuss this further.

To fill in the entered data in the text area in a HTML page, you must add a <textarea>-tag in a HTML <form>. Something like this:

<form method="post" action="<?= $PHP_SELF ?>">
<textarea name="output_str"><?= $_POST['output_str'] ?></textarea>
<input id="submit_save" name="submit_save" type="button" value="save" />
</form>

And remove this part of your code (that partially sends the data as a file):

    $text = file_get_contents($file);
    header("Content-type: application/text");
    header("Content-Disposition: attachment; filename=\"$file\"");
    echo $text; 

Note: that for safety reasons you should never use $_POST['output_str'] without sanitizing it (which I left out in the example to keep it clear).

Community
  • 1
  • 1
Veger
  • 37,240
  • 11
  • 105
  • 116
  • Thanks I think i am over my head with Php I want the textarea to save as a file the user can download firstly. then I will try and tackle the uploading part. – Ferg Jan 11 '13 at 14:09
0

I think you are trying something as this.

<?php

  if(isset($_POST['textfield'])) {
    $file = "output.txt";
    $output = htmlspecialchars ($_POST['textfield'] );
    file_put_contents($file, $output );
    $text = file_get_contents($file);

    header("Content-type: application/text");
    header("Content-Disposition: attachment; filename=\"$file\"");
    echo $text;
    exit;

  }
?>

<html>
<head>
    <title>Your app</title>
</head>

<body>
  <form method="POST">
    <textarea name="textfield"></textarea>
    <input id="submit_save" type="button" value="save" />
  </form>

</body>
</html>
  • thanks but it doesn't seem to work for me can my text area have an id and name? – Ferg Jan 11 '13 at 14:18
  • Yes, you can give a separate "id" and an "name" to the textarea. name is used by the form, id is used by the DOM if I am correct. – Nils Thiebosch Feb 27 '14 at 08:15