【发布时间】:2011-09-02 17:04:34
【问题描述】:
我正在尝试对我所写的表格的所有回复进行汇总,但我在制作时遇到了麻烦,以便每个回复都换行。我在下面有我的代码。我只是想让它更容易阅读,因为现在发生的事情是所有的响应都挤在一起,希望每个响应都在一个新的行上。我尝试了一些东西,并让它们在代码中注释以及结果是什么。提前致谢。
<?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更安全,除非您专门为另一个平台读取/写入文件。 -
谢谢你告诉我这件事,我会做一些研究
标签: php file-io webforms file-writing