Dominoed
Dominoed

Reputation: 123

How to retrieve <textarea> value with POST and save to a file?

On the page "index.php", I have the following html:

<form class="centerd" method="post" action="upload.php" enctype="multipart/form-data">
    <textarea id="html" type="text" name="html-text"></textarea>
    <input type="submit" value="upload"/>
</form>

On the page "upload.php", I have the following PHP code:

<?php
file_put_contents("/files/test.html", htmlspecialchars($_POST['html-text']));
?>
<h1>From the textbox:</h1>
<? echo htmlspecialchars($_POST['html-text']); ?>

I want the text from the textbox to show up after the <h1> tag, and I want /files/test.html to be created (/files/ already exists), and the text from the textbox to be put into the test.html file.

What actually happens, is whatever is in the textbox shows up after the <h1> tag, but /files/test.html is never created.

Upvotes: 0

Views: 1546

Answers (3)

VWT
VWT

Reputation: 11

you forgot the php!

not: <? echo ?> but: <?php echo ?>

Upvotes: 0

Kallehauge
Kallehauge

Reputation: 1

I'm sorry that I cannot comment on what Brannon said, I simply don't have reputation for it yet. But to the point:

There are two possibilities:

  1. As Brannon said -> your path seems to be wrong.
  2. You don't have permission to write (create) files in your directory. This can, in my experience, be a common error for Mac users who haven't provided permission in their htdocs folder.

Upvotes: 0

Brannon
Brannon

Reputation: 1336

It looks like "/files/test.html" is an absolute path. Have you tried "files/test.html"?

Upvotes: 4

Related Questions