After so long time, I was coding in PHP to write certain user inputs in a text file (.txt). Here are the codes that may find it helpful also.
To check if text file is empty
1 2 3 4 5 6 7 8 |
$text15_file = 'text15.txt'; $i = 0; if (trim(file_get_contents($text15_file)) == true) { $i = 1; } //Based on $i value, you can perform some action. |
To check if content already exists in text file
1 2 3 4 5 6 7 8 |
$ip = $_POST['txt_block_ip']; $all_ids = explode("\r\n", file_get_contents($text15_file)); if ( ! in_array($ip, $all_ids) ) //Proceed if content does not exists. { } |
To write on new line in text file if file is not empty
1 2 3 4 5 6 7 8 9 10 11 12 |
$text15_file = 'text15.txt'; $i = 0; if (trim(file_get_contents($text15_file)) == true) { $i = 1; } if($i==1) { $next_line = "\r\n"; fwrite($w_handle, $next_line); } |