Php append to file if the string is unique
append, file, php
Solution
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);
}
Problem
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