我正在嘗試將所有對所寫入表單的響應總計保留下來,但我無法將其設置爲每個響應需要換行。我的代碼如下。我只是想讓它更容易閱讀,因爲現在發生的情況是,所有的反應都卡在了一起,希望每一個反應都在一條新線上。我嘗試了一些東西,讓他們在代碼中註釋,結果如何。提前致謝。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