Handling Text file in PHP

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

$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

$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

$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); 
}