1

I want to check if a string exists within a text file and if it exists, return the message string exists. If it does not exist, add the string to the file and return string added.

I got it working with no message:

<?php

$path = '../test/usersBlacklist.txt';
$input = $_POST["id"];

if ($input) {
    $handle = fopen($path, 'r+');

    while (!feof($handle)) {
        $value = trim(fgets($handle));

        if ($value == $input) {
            return false;
        }
    }

    fwrite($handle, $input);
    fclose($handle);
    return true;
}

if (true) {
    echo 'added';
} else {
    echo 'exists';
}

?>
Kensuke
  • 13
  • 3
  • Using `return` outside of a function makes rather little sense. And `if (true)` is of course always true, because, well … true is true. You want to use a variable instead to store the status, and then check that variable in your if. And go read up on what the proper way is to exit from a loop. – CBroe May 24 '18 at 08:26
  • 2
    https://stackoverflow.com/questions/9059026/php-check-if-file-contains-a-string gives a better way of checking if a string is in a file you just need to add the correct logic around it. – Nigel Ren May 24 '18 at 08:29
  • Just wanted to write the same thing :) – Domenik Reitzner May 24 '18 at 08:29
  • Possible duplicate of [PHP check if file contains a string](https://stackoverflow.com/questions/9059026/php-check-if-file-contains-a-string) – Nick May 24 '18 at 08:32
  • Not full duplicate. Also asked for append if not found – Domenik Reitzner May 24 '18 at 08:33

3 Answers3

1

As @NigelRen mentioned use answer from this question and then use this to append:

if( strpos(file_get_contents($path),$input) !== false) {
    echo "found it";
}
else{
    file_put_contents($path, $input, FILE_APPEND | LOCK_EX);
    echo "added string";
}
Domenik Reitzner
  • 1,583
  • 1
  • 12
  • 21
0

This code has no sense, a solution is maybe to create a function :

function checkI($input) {
    $handle = fopen($path, 'r+');

    while (!feof($handle)) {
        $value = trim(fgets($handle));

        if ($value == $input) {
            return false;
        }
    }

    fwrite($handle, $input);
    fclose($handle);
    return true;
}

and then:

if (checkI($input)) {
    echo 'added';
} else {
    echo 'exists';
}

Your if(true) , it ll be always true.

0

If you are trying to append some value to a file that already has some data in it than it would be better to use "a+" flag instead of "r+"

As noted in the php docs:

'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended.

More info here: https://secure.php.net/manual/en/function.fopen.php

And also like CBroe said using return outside of a function won't help you a better way would be something like this:

$input = $_POST["id"];
function doesLineExist($input){
   $path = '../test/usersBlacklist.txt';  

   if ($input) {
       $handle = fopen($path, 'r+');

       while (!feof($handle)) {
         $value = trim(fgets($handle));

         if ($value == $input) {
           return false;
         }
       }

     fwrite($handle, $input);
     fclose($handle);
     return true;
   }
 }

$doesExist = doesLineExist($input);
if($doesExist){
    echo "Added"
 }else{
   echo "Exists"
 }
Igor Ilic
  • 1,370
  • 1
  • 11
  • 21
  • The function `doesLineExist()` returns true if the line doesn't exist and false if it does. This seems the wrong way round IMHO. More a case of is this the right name for a method than anything else. – Nigel Ren May 24 '18 at 08:36
  • It is, but I just used his code example to show to do it. It is up to him to alter it to suit him better. And yes I would use it like if line exist return true – Igor Ilic May 24 '18 at 08:37