Php 在文本文件中查找重复项
我不知道如何解决这个“feof():3不是有效的蒸汽资源”错误。我需要能够从用户的输入和交叉引用的列表中的数据。txt文件,并确保其不重复之前写。 有什么建议吗Php 在文本文件中查找重复项,php,file,text,Php,File,Text,我不知道如何解决这个“feof():3不是有效的蒸汽资源”错误。我需要能够从用户的输入和交叉引用的列表中的数据。txt文件,并确保其不重复之前写。 有什么建议吗 <!DOCTYPE> <html> <body> <form action="List.php" method="POST"> Name: <input type="text" name="name" /><br />
<!DOCTYPE>
<html>
<body>
<form action="List.php" method="POST">
Name: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
<input type="submit" name="save" value="Submit" /><br />
<input type="submit" name="open" value="Open in Text Editor" /><br />
</form>
<?php
$myArray = array();
if (isset($_POST['save']))
{
$name = $_POST['name'];
$email= $_POST['email'];
$toString = $_POST['name']. " ". $_POST['email'];
$fp = fopen("List.txt", 'w');
while (!feof($fp)) {
$line = fgets($fp);
if(strcmp($toString,$line)==0) {
echo "<h1>Duplicate entery.</h1>";
}
else {
$fp1 = fopen("List.txt", 'a');
fwrite($fp, $name);
fwrite($fp, $email);
echo "<h1>Success. U have been added to list.</h1>";
}
fclose($fp);
}
}
?>
</body>
</html>
名称:
电子邮件:
您正在关闭循环中的流。移动您的fclose($fp)
退出循环,并将流模式设置为w+
,如下所示:
$fp = fopen("List.txt", 'w+');
while (!feof($fp)) {
$line = fgets($fp);
if(strcmp($toString,$line)==0) {
echo "<h1>Duplicate entery.</h1>";
}
else {
$fp1 = fopen("List.txt", 'a');
fwrite($fp, $name);
fwrite($fp, $email);
echo "<h1>Success. U have been added to list.</h1>";
}
//fclose($fp); move this out
}
fclose($fp);
$fp=fopen(“List.txt”,“w+”);
而(!feof($fp)){
$line=fgets($fp);
if(strcmp($toString,$line)==0){
回声“重复肠道”;
}
否则{
$fp1=fopen(“List.txt”,“a”);
fwrite($fp,$name);
fwrite($fp,$email);
echo“成功。您已被添加到列表中。”;
}
//fclose($fp);将此移出
}
fclose($fp);
您只将其设置为只写此代码似乎可以工作,但它会覆盖现有数据。我可以在下一行保存数据的任何方式将fopen
的mode
参数替换为a+
。你可以阅读更多关于它的内容。