添加文本文件而不重复n1

时间:2018-03-11 16:13:50

标签: php

请帮助 修改此代码不应重复n1 将数据输入文本文件时 使用

添加新邮件时,请勿重复文本

 <?php
	
if(isset($_POST['send'])){
	
	$n1 = trim($_POST['n1']);
	$n2 = trim($_POST['n2']);

  $fileLocation = getenv("DOCUMENT_ROOT") . "/te.txt";
  $file = fopen($fileLocation,"a");
$n1 = $_POST['n1'];
$n2 = $_POST['n2'];

  fwrite($file,$n1.'|'.$n2."\r\n");
  fclose($file);
  
 

   }

?>

1 个答案:

答案 0 :(得分:0)

试试这个,

        <?php

    if(isset($_POST['send'])){

        $n1 = trim($_POST['n1']);
        $n2 = trim($_POST['n2']);

      $fileLocation = getenv("DOCUMENT_ROOT") . "/te.txt";
      $file = fopen($fileLocation,"r+");

  $content = fread($file, filesize($fileLocation));
 echo $content;
    if(strstr($content, "|")){
          fwrite($file,$n2."\r\n");
    }else{
      fwrite($file,$n1.'|'.$n2."\r\n");
    }

  fclose($file);

       }

    ?>