Is it possible to check if the string that is being added to file is not already in the file and only then add it? Right now I am using
        $myFile = "myFile.txt";
    $fh = fopen($myFile, 'a') or die("can't open file");
    $stringData = $var . "\n";
    fwrite($fh, $stringData);
    fclose($fh);
But I get many duplicates of $var values and wanted to get rid of them. Thank you
use this
$file = file_get_contents("myFile.txt");
if(strpos($file, $var) === false) {
   echo "String not found!";
   $myFile = "myFile.txt";
   $fh = fopen($myFile, 'a') or die("can't open file");
   $stringData = $var . "\n";
   fwrite($fh, $stringData);
   fclose($fh);
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With