Help with PHP - Counting and Removing lines

Discussion in 'PHP' started by FPForum, Jun 24, 2014.

  1. #1
    hey everyone...so here is what i'm trying to do.

    I plan to setup a cron job which will run once every night. The cron will call a PHP file that should count the number of lines in a text file. Once the number of lines reaches X (for example, 75) it would then either remove the text file and recreate it - or just wipe the file clean and remove all 75 lines.

    I'm not too good with PHP programming but I did find some code to count the number of lines. What i'm having problems with now would be the if statement that remove the file or the lines...Any help or links to a good tutorial would be nice!
     
    FPForum, Jun 24, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Why, exactly, are you using PHP for this job? Wouldn't a simple script line do the job just fine?
     
    PoPSiCLe, Jun 24, 2014 IP
  3. FPForum

    FPForum Notable Member

    Messages:
    4,172
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    225
    Digital Goods:
    2
    #3
    I wouldn't know how to do this via a cron command. I was thinking there are probably more people out there who would know how to do this via PHP as opposed to a one-line command.
     
    FPForum, Jun 24, 2014 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #4
    You just need to
    1. open the file, get the contents http://www.php.net//manual/en/function.file-get-contents.php
    2. explode the contents into an array $bits = explode("\n", $contents);
    3. check the size of the array if (count($bits) > 75) {}
    4. unlink the file to delete
    5. create the new empty file
    but...

    what is the role of the file? why does it only get checked once a day?

    without knowing what you are actually trying to do I'd recommend using a table in a database and then just selecting from the database based on the date and whatever other criteria exist with a limit of x (ie 75). Then once in a blue moon you can pop in and delete the old records from the table.
     
    sarahk, Jun 24, 2014 IP
  5. FPForum

    FPForum Notable Member

    Messages:
    4,172
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    225
    Digital Goods:
    2
    #5
    Thanks for your help sarahk, but I think I figured it out.

    First, open and count the lines
    
    $file="/path/to/your/file";$linecount = 0;$handle = fopen($file, "r");
    while(!feof($handle)){$line = fgets($handle);$linecount++;
    }fclose($handle);
    
    Code (markup):
    Then, determine if it's 75 lines and wipe:
    
    if($linecount >= 75) {$fh = fopen($file, 'w') or die("can't open file");fwrite($fh, "");fclose($fh);
    }
    
    Code (markup):
    Thanks..
     
    FPForum, Jun 24, 2014 IP