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

大城市里の小女人 提交于 2019-12-09 07:18:07

问题


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


回答1:


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! :D

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




回答2:


Like Geek Num 88 says, using AJAX (or even Websockets) will prevent reloading the page. But you're already almost there with this code. The file_put_contents first argument should be a string pointing to the path of the file. What you are giving it now is a resource of an opened file.

So just remove the fopen command and just assign the filename to $file.

Small tip: reading the file is just as simple with the equivalent file_get_contents. I do see some security implications with this implementation though. So always filter input and escape output. But that's a whole other subject.

Happy coding!




回答3:


The write part only needs to execute if the submit is from the first form. The code below will write the file if submitted from first form, and will read the file if submitted from second form.

if (isset($_POST)){

   if ($_POST['submit'] == "Save Text to Server"  && !empty($_POST['filename'])) {
        //Save File
        $file = fopen($_POST['filename'] & ".txt","r+");
        $text = $_POST["textdata"];
        file_put_contents($file, $text);
        fclose($file);
    }

    if ($_POST['submit'] == "Submit File Request") {
         //Open File
         $file = fopen($_POST['filename'] & ".txt", "r") or exit("Unable to open file.");
         while(!feof($file)){
            echo fgets($file). "<br />";
          }
         fclose($file);
     }
}

If you need to do this without reloading the page, you need to use an Ajax post to the above code, from the form.




回答4:


$file = fopen($_POST['filename'] & ".txt","r+");

Concatenation character in PHP is . (dot) instead of &. The filename is defined by user so you should call fopen with mode 'w' which will try to create the file in case it doesn't exist.

Testing isset($_POST) always returns true, rather test isset($_POST['filename']).



来源:https://stackoverflow.com/questions/12773838/how-can-i-save-txt-files-from-a-html-textarea-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!