Skip to content
Advertisement

How can I save TXT Files from a HTML textarea, using PHP?

I am trying to make a text file storage system for my website.

Here is what I have so far.

I have gotten some parts to work, but am getting many more errors after making a few changes I thought would help.

I am trying to accomplish this task without changing pages or url.

<!DOCTYPE HTML>
<html>
    <body>
        <?php
    if (isset($_POST)){
        //Save File
        $file = fopen($_POST['filename'] & ".txt","r+");
        $text = $_POST["textdata"];
        file_put_contents($file, $text);
        fclose($file);
        //Open File
        $file = fopen($_POST['filename'] & ".txt", "r") or exit("Unable to open file.");
        while(!feof($file)){
            echo fgets($file). "<br />";
        }
        fclose($file);
    }

    echo '
    <form name="savefile" method="post" action="' . $_SERVER['PHP_SELF'] . '">
        File Name: <input type="text" name="filename" value=""><br/>
        <textarea rows="20" cols="100" name="textdata"></textarea><br/>
        <input type="submit" name="submit" value="Save Text to Server">
</form>
    <br/><hr style="width: 100%; height: 4px;"><br/>
    <form name="openfile" method="post" action="' . $_SERVER['PHP_SELF'] . '">
        Open File: <input type="text" name="filename" value="">
        <input type="submit" name="submit" value="Submit File Request">
</form>';
    ?>
    </body>
<html>

If the only way is to have it redirect to a php page, then send it back, that is fine, but I have no clue how to do that, (even though its probably A LOT simpler)

Thanks for any help or advice you can provide me!

-Jake

Advertisement

Answer

First of all, thanks sooo much for the help!

I really appreciate it when those people who are more experienced can help out a newbie once in a while.

After spending some time tinkering with my code with some of the suggestions from you, I have finally gotten it to work! 😀

I’ll leave the final code here in case anyone else stumbles upon this topic with a similar issue.


<!DOCTYPE HTML>
<html>
<body style="background-color: rgb(225,225,225)">
    <form name="savefile" method="post" action="">
        File Name: <input type="text" name="filename" value=""><br/>
        <textarea rows="16" cols="100" name="textdata"></textarea><br/>
        <input type="submit" name="submitsave" value="Save Text to Server">
</form>
    <br/><hr style="background-color: rgb(150,150,150); color: rgb(150,150,150); width: 100%; height: 4px;"><br/>
    <form name="openfile" method="post" action="">
        Open File: <input type="text" name="filename" value="">
        <input type="submit" name="submitopen" value="Submit File Request">
</form>
    <br/><hr style="background-color: rgb(150,150,150); color: rgb(150,150,150); width: 100%; height: 4px;"><br/>
    File Contents:<br/>
    <?php
    if (isset($_POST)){
        if ($_POST['submitsave'] == "Save Text to Server"  && !empty($_POST['filename'])) {
            if(!file_exists($_POST['filename'] . ".txt")){
                $file = tmpfile();
            }
            $file = fopen($_POST['filename'] . ".txt","a+");
            while(!feof($file)){
                $old = $old . fgets($file). "<br />";
            }
            $text = $_POST["textdata"];
            file_put_contents($_POST['filename'] . ".txt", $old . $text);
            fclose($file);
        }

        if ($_POST['submitopen'] == "Submit File Request") {
            if(!file_exists($_POST['filename'] . ".txt")){
                exit("Error: File does not exist.");
            }
            $file = fopen($_POST['filename'] . ".txt", "r");
            while(!feof($file)){
                echo fgets($file). "<br />";
            }
            fclose($file);
        }
    }
    ?>
</body>
</html>

Hope this helps!

-Jake

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement