Huynh Bao
Huynh Bao

Reputation: 121

How to save textarea to file php

I want to save text in textarea to file but it don't save, when I enter text into textarea and press button "Save", it look like at the firt, no change.

My code here:

<?php   
    if($_POST['textpackages']){
        $content = $_POST['content'];
        $file = "http://baokool.net/Packages";
        $Saved_File = fopen($file, 'a+');
        fwrite($Saved_File, $content);
        fclose($Saved_File);
    } else {
        echo 'ERROR';
    }
?>

<form action="test.php" method="post">
    <textarea name="content">
        <?php
            echo file_get_contents("http://baokool.net/Packages");
        ?>
    </textarea>
    <input type="submit" name="submit" value="Save">
</form>

Please help me. Thank you very much.

Sorry because my English is bad.

Upvotes: 2

Views: 3203

Answers (2)

Y.Hermes
Y.Hermes

Reputation: 459

If this script is running on the same Server where you want to save the file, you can write a string into a file with "file_put_contents"(php.net). Just think about the permissions you need to save a file, thats probably why it isn't working. If you are trying to save it through HTTP, I think this is not possible.

Upvotes: 0

Mathias-S
Mathias-S

Reputation: 805

You can't edit a file through HTTP like you're trying to do. You need to use a local file, i.e.:

$content = $_POST['content'];
$file = "yourfile"; // cannot be an online resource
$Saved_File = fopen($file, 'a+');
fwrite($Saved_File, $content);
fclose($Saved_File);

Upvotes: 2

Related Questions