php PHP从输入写入文件到txt
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14998961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP write file from input to txt
提问by user2094841
I've searched around this site for an answer but couldnt find any.
我在这个网站上搜索了答案,但找不到任何答案。
I have a form and I'd like to get the contents of the input written into a txt file. To make it simple I just wrote a simple form and a script but it keeps getting me a blank page. Here is what I got
我有一个表单,我想将输入的内容写入 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>
and here is my PHP file
这是我的 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
?>
回答by Floby
Your form should look like this :
您的表单应如下所示:
<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>
and the PHP
和 PHP
<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "\r\n";
$ret = file_put_contents('/tmp/mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
I wrote to /tmp/mydata.txtbecause this way I know exactly where it is. using data.txtwrites to that file in the current working directory which I know nothing of in your example.
我写信给/tmp/mydata.txt因为这样我就知道它在哪里。usingdata.txt写入当前工作目录中的该文件,我在您的示例中对此一无所知。
file_put_contentsopens, writes and closes files for you. Don't mess with it.
file_put_contents为您打开、写入和关闭文件。不要惹它。
Further reading: file_put_contents
进一步阅读: file_put_contents
回答by Yogesh Suthar
The problems you have are because of the extra <form>you have, that your data goes in GETmethod, and you are accessing the data in PHPusing POST.
您遇到的问题是因为您拥有的额外<form>数据,您的数据进入GET方法,并且您正在PHP使用POST.
<body>
<!--<form>-->
<form action="myprocessingscript.php" method="POST">
回答by Lobo
A possible solution:
一个可能的解决方案:
<?php
$txt = "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'];
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
}
?>
You were closing the script before close de file.
您在关闭文件之前关闭了脚本。
回答by Naryl
If you use file_put_contents you don't need to do a fopen -> fwrite -> fclose, the file_put_contents does all that for you. You should also check if the webserver has write rights in the directory where you are trying to write your "data.txt" file.
如果您使用 file_put_contents,则不需要执行 fopen -> fwrite -> fclose,file_put_contents 会为您完成所有操作。您还应该检查网络服务器是否在您尝试写入“data.txt”文件的目录中具有写入权限。
Depending on your PHP version (if it's old) you might not have the file_get/put_contents functions. Check your webserver log to see if any error appeared when you executed the script.
根据您的 PHP 版本(如果它是旧的),您可能没有 file_get/put_contents 函数。检查您的网络服务器日志以查看执行脚本时是否出现任何错误。

