【发布时间】:2015-01-11 15:18:27
【问题描述】:
我试图让用户在文本框中写一些东西。然后将文本写入名为“list.txt”的文件中的新行。然后我希望在网站上阅读整个文件。这样它就像一个聊天室或其他东西。问题是,现在它告诉我“无法打开文件”。我已经检查了文件的权限,并且每种类型的用户都具有完全访问权限。 List.txt 也与 php 文件位于同一目录中。此外,当它能够打开文件时,它不会将其写入新行。我知道这可以使用 "\n" 或 PHP.EOL 来完成,但它们似乎都不起作用,有时它们会阻止网站本身运行。请帮我弄清楚是怎么回事,谢谢。
<html>
<p>
<form name="form1" method="post" action="test.php">
<input name="text" type="text">
<input type="submit" value="send" method="post">
</p>
<p>
<?php
$file = fopen("list.txt") or die ("Unable to open file!");
$text = $_POST['text'];//where the hell do you put the new line indicator: "\n"//
fwrite($file, $text) or die ("Cannot write!");
$print = fread($file,filesize("list.txt", "a+"));
fclose($file);
echo $print;
?>
</p>
<?php //figure out how to make this a button that clears the file
/*
$fp = fopen("list.txt", "r+");
// clear content to 0 bits
ftruncate($fp, 0);
//close file
fclose($fp);*/
?>
</html>
【问题讨论】:
-
为什么不使用 file_put_contents
-
看起来您在
filesize()函数中使用了访问修饰符。你只需要一个参数,文件名。