Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无重复的PHP编写_Php_Fopen_Fwrite - Fatal编程技术网

无重复的PHP编写

无重复的PHP编写,php,fopen,fwrite,Php,Fopen,Fwrite,我想以这样一种方式编写代码,它可以将任何输入写入并保存到.TXT文件中。然后应该有功能,将搜索整个文件的txt,如果一个文本或字已经写或保存之前,它应该带来错误 如果有人写了爱,而另一个人想再次输入爱,它应该会带来一条错误消息 这是我的密码 <?php $name=$_POST['name']; $myfile= fopen("man.txt" , "a+") or die("Unable to open file!"); fwrite($myfile, "Name:".$name. "\

我想以这样一种方式编写代码,它可以将任何输入写入并保存到.TXT文件中。然后应该有功能,将搜索整个文件的txt,如果一个文本或字已经写或保存之前,它应该带来错误

如果有人写了爱,而另一个人想再次输入爱,它应该会带来一条错误消息

这是我的密码

<?php
$name=$_POST['name'];
$myfile=
fopen("man.txt" , "a+") or
die("Unable to open file!");
fwrite($myfile, "Name:".$name. "\r\n")
?> 
在这里,我使用in_array函数检查了一个数组,我们为提交的名称填充了$users

如果找到该名称,则表示存在回显名称。如果找不到,则回显“否”,并添加名称

<?php 

$name = $_POST['name'];
if (empty($name)) { echo "Please enter your name"; } else $name=$name;

$myfile= fopen("mam.txt" , "a+") or die("Unable to open file!");

$path="mam.txt";

if (file_exists($path)) {
    $contents = file_get_contents($path);
    $contents = explode("\r\n", $contents);

    $users = array();

    foreach ($contents as $value) {
        $users[] = $value;
    }
    // Search the file for the submitted name
    if (in_array($name, $users)) {
        // The name already exists
        echo "Name exist";
    } else {
        // The name doesnt exist
        echo "NO"; 
        // Add the new name
        fwrite($myfile, $name. "\r\n");
    }
} 
?>

到目前为止,您尝试了什么代码/方法?这里是另一个代码,非常感谢。成功了。如果我刷新页面时在TeX字段中没有任何内容,它将告诉我name exist。它将空空间视为数组。帮助