Open .txt File Peel Off One Line and Save File with PHP

This code snippet will demonstrate how to use PHP to open a .txt file or any other plain text  file such as .php, .html, .xml etc., retrieve the text in the first line of the file, delete that line and save the file. We are effectively peeling off one line from the file and saving it using pure PHP. Here is how:

<?php
//included in index-cron.php to peel off one keyword from keywords.txt file and use it saving rest back to file.
//turn debugging on/off:
$debug = true;
//read in keyword phrases from keywords.txt file:
$lines = “”;
$file = fopen(“keywords.txt”, “r”) or exit(“Unable to open keywords.txt file!”);
//Output a line of the file until the end is reached:
$line_no=0;
    while(!feof($file)) {
    $line_no++;
    $line = fgets($file);
    $alllines .= $line.”<br>”;
    //code to later delete first line from keywords.txt file:
        if($line_no > 1){//if over line 1, store in writelines var to write back to file later.
        $writelines .= $line;
        }else{//else line no. is NOT over 1, so store line in cur_keywrd var:
        $curkey = trim($line);
        $cur_keywrd = $curkey;
        }//end else line is under 1 so put in cur_keywrd var.
    }//end while reading in lines from keywords.txt file
fclose($file);
if($debug) echo “<h2>Keyword taken from keywords.txt file: $cur_keywrd</h2><hr>”;

//now rewrite keywords.txt file with all but first 1 lines:
$kfile = ‘keywords.txt’;
$fh = fopen($kfile, ‘w’) or die(“ERROR! Cannot open $kfile file for saving Keywords back to it!”);
fwrite($fh, $writelines);
fclose($fh);
if($debug) echo “keyword $cur_keywrd Deleted from keywords.txt file!<hr>”;
?>

There you have it! Copy and paste the above code into a .php file named fetch_keywords.php. Now let’s test it…

Make a file named keywords.txt and add the following test content to it:

test keyword 1
test keyword 2
test keyword 3
test keyword 4
test keyword 5
test keyword 6
test keyword 7
test keyword 8
test keyword 9
test keyword 10
test keyword 11
test keyword 12

Save keywords.txt and upload it to the same directory as fetch_keywords.php then open fetch_keywords.php in a browser and you will see this:

kwrfds

If your screen looks like the above shot, then you’re doing great! If not, debugging time, but it should work. See where it says “test keyword 1”? That’s the keyword it peeled from the keywords.txt file. Now try refreshing the page and it will increment to “test keyword 2”. Refresh one more time just for fun and see “test keyword 3” appear. Now download keywords.txt and open it and you’ll be able to verify that it did indeed remove the first few lines from the file. This is very usefull if you need to use PHP to read in keywords from a file or domains, phone numbers, names, etc from a file to use the data in your script. Have fun with this and if you need to read multiple lines, the first 100 lines in from a file and save it after deleting those 100 lines, read on, I’ll show you how to do that too!

How to read the first 100 lines from a file, delete them and save the file

The next PHP snippet I”ll show you was used to read from a huge list of domain names, take the first 100 domains, use them in my script and save the unused domains back to the .txt file.

The code is similar to the above code with some important changes as you can see below:

<?php
//script to peel of first 100 domains from doms.txt file and save it
//turn debugging on/off:
$debug = true;
//read in domain names from doms.txt file:
$lines = “”;
$file = fopen(“doms.txt”, “r”) or exit(“Unable to open doms.txt file!”);
//Output a line of the file until the end is reached:
$line_no=0;
    while(!feof($file)) {
    $line_no++;
    $line = fgets($file);
    $alllines .= $line.”<br>”;
    //code to later delete first 100 lines from doms.txt file:
        if($line_no > 100){//if over line 100, store in writelines var to write back to file later.
        $writelines .= $line;
        }else{//else line no is NOT over 100, so store line in dom array:
        $uselines .= $line.”<br>”;
        $curURL = trim($line);
        //if cur URL doesn’t conain a TLD, add .com
            if(strpos($curURL,”.”)===false){
            $curURL = $curURL.”.com”;
            }//end if curURL doesn’t have a dot
        $dom_array[] = $curURL;
        }//end else line is under 100 so put in dom array.
    }//end while reading in lines from doms.txt file
fclose($file);
if($debug) echo”<h2>Domains taken from dom.txt file:</h2>$uselines<hr>”;

//now rewrite doms.txt file with all but first 100 lines:
$domFile = ‘doms.txt’;
$fh = fopen($domFile, ‘w’) or die(“ERROR! Cannot open $domFile file for saving domains back to it!”);
fwrite($fh, $writelines);
fclose($fh);
if($debug) echo”Above Domains Deleted from doms.txt file!<hr>”;
?>

Leave a Reply

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