小编use*_*841的帖子

PHP将文件从输入写入txt

我在这个网站上搜索了一个答案,但找不到任何答案.

我有一个表单,我想将输入的内容写入txt文件.为了简单起见,我只写了一个简单的表单和一个脚本,但它一直让我得到一个空白页面.这就是我得到的

<html>
<head>
    <title></title>
</head>
<body>
    <form>
        <form action="myprocessingscript.php" method="post">
        <input name="field1" type="text" />
        <input name="field2" type="text" />
        <input type="submit" name="submit" value="Save Data">
    </form>
    <a href='data.txt'>Text file</a>
</body>
Run Code Online (Sandbox Code Playgroud)

这是我的PHP文件

<?php
$txt = "data.txt"; 
$fh = fopen($txt, 'w+'); 
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
   $txt=$_POST['field1'].' - '.$_POST['field2']; 
   file_put_contents('data.txt',$txt."\n",FILE_APPEND); // log to data.txt 
   exit();
}
    fwrite($fh,$txt); // Write information to the file
    fclose($fh); // Close the file
    ?>
Run Code Online (Sandbox Code Playgroud)

php

14
推荐指数
2
解决办法
12万
查看次数

标签 统计

php ×1