Configuration guides for IT Administrators

Handling Text file in PHP

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

 

One response to “Handling Text file in PHP”

  1. Briantet avatar

    Here on this portal, visitors can come across an abundance of helpful information.
    Our portal provides comprehensive articles on various topics.
    If you are a beginner or an expert, users will find something valuable.
    Much of the data is conveniently categorized, making it easy to access what you’re looking for.
    https://jamaissansmacravate.fr/comment-bien-choisir-la-coupe-de-son-polo-en-fonction-de-sa-morphologie/

Leave a Reply

Your email address will not be published. Required fields are marked *

Lucas Brey

I’m Lucas Brey, a travel blogger sharing practical guides, hidden gems, and honest tips from the road. Follow along for smart itineraries, great food finds, and stories worth bookmarking.

Tags