我正在尝试创建一个页面,将文本随时间记录到文件中
index.html:
<html>
<form action="process.php" method="POST">
<input name="field1" type="text" />
<!-- <input name="field2" type="text" /> -->
<input type="submit" name="submit" value="Save">
</form>
</html>
process.php:
<?php
if(isset($_POST['field1'])) {
$date = date('Y-m-d His');
$data = "n" + "<pre>$date</pre>" + $_POST['field1'];
# if(isset($_POST['field1']) && isset($_POST['field2'])) {
# $data = $_POST['field1'] . '-' . $_POST['field2'] . "n";
# $filename = date('YmdHis').".txt";
$filename = "log.txt";
if (!file_exists($filename)) {
$fh = fopen($filename, 'w') or die("Cannot create file");
}
$ret = file_put_contents($filename, nl2br($data), FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to $filename";
}
}
else {
die('No post data to process');
}
?>
log.txt:
0
当我输入";测试";log.txt显示";0〃;。
如何让它在每次输入时都在新行上输出时间和文本。
我修复了它。
我删除了+并用句点替换,我删除了:
if (!file_exists($filename)) {
$fh = fopen($filename, 'w') or die("Cannot create file");
}
因为不需要,我删除了预标签,因为它们不需要。
process.php:
<?php
if(isset($_POST['field1'])) {
$date = date('Y-m-d ');
$data = "n" . "$date" . $_POST['field1'];
$filename = "log.txt";
$ret = file_put_contents($filename, $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to $filename";
}
}
else {
die('No post data to process');
}
?>
现在它可以正确地输出数据。
log.txt:
2021-06-22 test