【问题标题】:Write to file using form input PHP使用表单输入 PHP 写入文件
【发布时间】:2021-10-22 17:55:46
【问题描述】:

我正在做的项目的目标是制作不同的 PHP 表单来创建、写入、读取和删除文件。这个(insert.php)是我的“写入”页面,可以正确打开文件,但是当按下“fileInsert”按钮时,它会刷新页面并在写入或显示成功消息之前关闭打开的文件。任何帮助或建议表示赞赏

<!DOCTYPE html>
<html>

    <head>
        <title>File Creator</title>
    </head>

    <body>
        <h1>File Creator</h1>
        <form action="" method="post">
            <label for="fileName">Enter the name of the file you would like to open including the extension:</label><br />
            <input type="text" name="fileName"><br />
            <input type="submit" value="Open File" name="fileOpen">
        </form>
    </body>

    <?php

    

    if(isset($_POST['fileOpen'])) {

        $fileName = $_POST['fileName'];
        echo str_ireplace("None", $fileName, "<h3>Currently open: None</h3><br />");

        if (is_file($fileName)) {
            echo "<form action='' method='post'>";
            echo "<label for='fileInput'>File is open!<br />Insert up to 40 characters:</label><br />";
            echo "<input type='text' name='fileInput' maxlength='40'><br />";
            echo "<input type='submit' value='Submit' name='fileInsert'><br /><br />";
            echo "</form>";

            if(isset($_POST['fileInsert'])) {

                $fileInput = $_POST['fileInput'];

                $myfile = fopen($fileName, "w");

                fwrite($myfile, $fileInput);
                fclose($myfile);

                echo "Done! <br />";
            
            }
        }
        else {
            echo "File does not exist. <br /><br />";

            echo "<a href='start.php'><button>Return to Homepage</button></a>";
        }
        
    }

    else {
        echo "<h3>Currently open: None</h3><br />";
    }

    ?>

</html>```

【问题讨论】:

  • fileOpenfileInsert 不能同时设置,它们在不同时间提交的不同表单上。您要么需要一个带有文件名和输入的表单,要么需要一个两步过程,第二步打开并写入。

标签: php forms file-handling file-writing


【解决方案1】:

每次将表单发布到同一页面或任何其他页面...我们在 $_POST 中只有最后一次发布数据。

这很简单...因为您在同一页面中提交另一个表单...第二次提交再次重新加载页面...但是现在...在 $_POST['fileOpen' ] 因为第 2 篇文章只包含第 2 篇文章的数据,所以这里有一个固定版本供您使用:

<!DOCTYPE html>
<html>

    <head>
        <title>File Creator</title>
    </head>

    <body>
        <h1>File Creator</h1>
        <form action="" method="post">
            <label for="fileName">Enter the name of the file you would like to open including the extension:</label><br />
            <input type="text" name="fileName"><br />
            <input type="submit" value="Open File" name="fileOpen">
        </form>
    </body>

    <?php

    if(isset($_POST['fileInsert'])) {

        $fileInput = $_POST['fileInput'];

        $myfile = fopen($_POST['fileName'], "w");

        fwrite($myfile, $fileInput);
        fclose($myfile);

        echo "Done! <br />";

    }

    if(isset($_POST['fileOpen'])) {

        $fileName = $_POST['fileName'];
        echo str_ireplace("None", $fileName, "<h3>Currently open: None</h3><br />");

        if (is_file($fileName)) {
            echo "<form action='' method='post'>";
            echo "<label for='fileInput'>File is open!<br />Insert up to 40 characters:</label><br />";
            echo "<input type='text' name='fileInput' maxlength='40'><br />";
            echo "<input type='hidden' name='fileName' value='$fileName' >";
            echo "<input type='submit' value='Submit' name='fileInsert'><br /><br />";
            echo "</form>";


        }
        else {
            echo "File does not exist. <br /><br />";

            echo "<a href='start.php'><button>Return to Homepage</button></a>";
        }

    }

    else {
        echo "<h3>Currently open: None</h3><br />";
    }

    ?>

</html>

我将 $_POST['fileInsert'] 剪切到 $_POST['fileOpen'] 之外,因为当我们得到这个时我们没有得到那个,并且我在其中添加了一个隐藏的输入字段 我在 $_POST['fileInsert'] 的 if 中使用的名为 fileInput 的第二种形式我需要知道自第一次发布以来也丢失的文件名!

此方法还将在完成后的末尾打印“当前打开:无”!

顺便说一句:这将覆盖您的文件!使用 'a' 而不是 'w' 来添加文件。

【讨论】:

    猜你喜欢
    • 2015-03-15
    • 2016-03-30
    • 2018-11-08
    • 1970-01-01
    • 2023-04-09
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多