Home » Php » Return message if string is found or added added to file

Return message if string is found or added added to file

Posted by: admin June 30, 2018 Leave a comment

Questions:

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';
}

?>
Answers: