I believe fgets .
$handle = fopen($path, 'r+'); // open the file for r/w while (!feof($handle)) { // while not end $value = trim(fgets($handle)); // get the trimmed line if ($value == $input) { // is it the value? return; // if so, bail out } // } // otherwise continue fwrite($handle, $input); // hasn't bailed, good to write fclose($handle); // close the file
This answer is based solely on the fact that you added a new line ( "\n" ) to your code, so fgets will work here. This may be preferable to transferring the entire file to memory using file_get_contents() , simply because the file size may be forbidden.
Alternatively, if the values ββare not limited to a newline, but are a fixed length, you can always use the $length fgets() argument to pull out only the $n characters (or use fread() to print t26> bytes exactly)
Dan
source share