douchun3680 2012-10-07 23:49
浏览 319
已采纳

如何使用PHP从HTML文本区域保存TXT文件?

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

  • 写回答

4条回答 默认 最新

  • doulu7921 2012-10-08 04:21
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)