我正在尝试将所有对所写入表单的响应总计保留下来,但我无法将其设置为每个响应需要换行。我的代码如下。我只是想让它更容易阅读,因为现在发生的情况是,所有的反应都卡在了一起,希望每一个反应都在一条新线上。我尝试了一些东西,让他们在代码中注释,结果如何。提前致谢。PHP,写入文件时的换行符
<?php
if (isset($_POST['sometext']))
{
$myFile = "testFile.txt";
$thetext=$_POST['sometext'] ;//added + "\n" here but all response turned to 0
writemyfile($myFile,$thetext,"a");
} else
{
$thetext="Enter text here";
}
function readmyfile($thefile)
{
$file = fopen($thefile, "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
}
function writemyfile($thefilename,$data,$mode)
{
$myfile=fopen($thefilename,$mode);
fwrite($myfile, $data); // added + "\n" here and responses turned 0
fclose($myfile);
}
?>
<html>
<head>
<title> Zain's Test Site</title></head>
<body>
<form method="post" action="<?php echo $php_self ?>">
<input type="text" name="sometext" value="<?php echo $thetext ?>" >
<input type="submit" name="Submit" value="Click this button">
</form>
<?php readmyfile("testFile.txt"); ?>
</body>
PHP有一个常量,'PHP_EOL',它包含你的主机平台的行尾字符。使用它比'\ n'更安全,除非您专门为其他平台读取/写入文件。 – 2011-05-26 15:28:23
谢谢你告诉我,我会做一些研究 – Zaask 2011-05-26 15:36:37