无法在php中写入txt文件

时间:2016-10-07 19:05:13

标签: php linux apache file-io

我试图简单地用PHP中的文本框中输入的数字覆盖txt文件。 PHP和文本文件都在我的apache2服务器的html目录中。每次执行它时都会显示die()字符串。我也尝试使用fwrite()得到相同的结果。任何帮助将非常感激。

<HTML>
<TITLE>Home Automation Interface</TITLE>
<BODY>
    <H1>Air Conditioning Power(Relay 1)</H1>
    <H2>Turn AC On/Off</H2>
    <H3>
        <p> 
            <form action="relayToggle.php" method="get"> 
            <input type="submit" value="AC Power">
            </form>
        </p>
        <form method= "post" action = "homeInter.php">
        <p>
            <label for="acTemp">AC temperature (F):</label><br/>
            <input type="text" id="temp" name="temp">
        </p>
        <button type="submit" name="submit" value="send">Change Temperature</button>
        </form>
        <p><strong>
          <?php

              $temp= $_POST['temp'];
              echo $temp;
              file_put_contents("/var/www/html/temp.txt", (string)$temp) or die("Unable to open file!");
           ?>
                    }
                }
            ?>
            </strong>
            </p>
        </H3>
    </BODY>
</HTML>

1 个答案:

答案 0 :(得分:0)

包装您的PHP代码在有效的检查器中查看表单是否已提交

像这样:

if ($_POST):
    //All functions go here
endif;

如果文件与PHP文件位于同一目录中,只需将目录更改为文件名。文件扩展名 file_put_content()

相关问题