簡體   English   中英

使用表單輸入 PHP 寫入文件

[英]Write to file using form input PHP

我正在做的項目的對象涉及制作不同的 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>```

每次將表單發布到同一頁面或任何其他頁面時……我們只有 $_POST 中的最后發布數據。

這很簡單...因為您在同一頁面中提交另一個表單...第二次提交再次重新加載頁面...但現在... $_POST['fileOpen'] 中沒有什么可處理的,因為第二篇文章只包含第二篇文章的數據,所以這里有一個固定版本給你:

<!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'] 之外,因為當我們得到這個時我們沒有得到那個,並且我在我使用的第二個名為 fileInput 的表單中添加了一個隱藏的輸入字段在 $_POST['fileInsert'] 的 if 中,我需要知道自第一次發布以來也丟失的文件名!

此方法還將在完成后在末尾打印“Currently open:none”!

順便說一句:這會覆蓋你的文件! 使用 'a' 而不是 'w' 添加到文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM