2

How to put a limit for writing in a file, if it hit the limit then remove the last line..

As example here's a file:

Line 3
Line 2
Line 1

i want to max line it for 3 lines only.. so when i write a new line using any append functions it removes the last line.. Let's say i just wrote a new line ( Line 4 ).. so it goes to the last one and remove it, result should be :

Line 4
Line 3
Line 2

And for a new written line (Line 5):

Line 5
Line 4
Line 3

numeric lines is not required, i just want to remove the last line if there's a new added line via an append functions (file_put_contents / fwrite) and max it by 3 or a specific number i give

Osa
  • 1,922
  • 7
  • 30
  • 51

4 Answers4

3

You can try

$max = 3;
$file = "log.txt";
addNew($file, "New Line at : " . time());

Function Used

function addNew($fileName, $line, $max = 3) {
    // Remove Empty Spaces
    $file = array_filter(array_map("trim", file($fileName)));

    // Make Sure you always have maximum number of lines
    $file = array_slice($file, 0, $max);

    // Remove any extra line 
    count($file) >= $max and array_shift($file);

    // Add new Line
    array_push($file, $line);

    // Save Result
    file_put_contents($fileName, implode(PHP_EOL, array_filter($file)));
}
Baba
  • 94,024
  • 28
  • 166
  • 217
  • this one works great but it appends on bottom, i want to add new lines on top, i tried array_unshift but doesnt seem to be working correctly – Osa Oct 22 '12 at 18:27
  • Do you still need a solution ?? – Baba Oct 22 '12 at 20:56
  • nope its alright, thanks.. if you have a time can u include the add to top instead of bottom ? – Osa Oct 23 '12 at 00:14
  • Solution good only for small files. I would not recommend it for bigger files because whole file is read into array (memory consumption). For large files use `fgets()` to count lines and `fwrite()`. Prepend in such way is not elegant but possible: http://stackoverflow.com/questions/3332262/how-do-i-prepend-file-to-beginning Slower solution, but has small memory consumption. – imclickingmaniac Feb 11 '14 at 14:39
1

Here's one way:

  1. Use file() to read the file's lines into an array
  2. Use count() to determine if there's more than 3 elements in the array. If so:
    1. Remove the last element of the array with array_pop()
    2. Use array_unshift() to add an element (the new line) to the front of the array
    3. Overwrite the file with the lines of the array

Example:

$file_name = 'file.txt';

$max_lines = 3;              #maximum number of lines you want the file to have

$new_line = 'Line 4';               #content of the new line to add to the file

$file_lines = file($file_name);     #read the file's lines into an array

#remove elements (lines) from the end of the
#array until there's one less than $max_lines
while(count($file_lines) >= $max_lines) {    
    #remove the last line from the array
    array_pop($file_lines);
}

#add the new line to the front of the array
array_unshift($file_lines, $new_line);

#write the lines to the file
$fp = fopen($file_name, 'w');           #'w' to overwrite the file
fwrite($fp, implode('', $file_lines)); 
fclose($fp); 
user428517
  • 4,132
  • 1
  • 22
  • 39
  • normally i would never do this, but here you go. haven't tested it but that's the basic idea. i originally gave you a pretty clear step-by-step process - next time why not try to figure it out on your own, then ask another question if you get stuck? that's the way to learn. – user428517 Oct 22 '12 at 18:32
0

Try this.

<?php

// load the data and delete the line from the array 

$lines = file('filename.txt'); 
$last = sizeof($lines) - 1 ; 
unset($lines[$last]); 

// write the new data to the file 

$fp = fopen('filename.txt', 'w'); 
fwrite($fp, implode('', $lines)); 
fclose($fp); 

?>
Abhishek Saha
  • 2,564
  • 1
  • 19
  • 29
  • it only removes the last line, i want to keep writing till it hits the max, if max hit then remove the last line and add the new one on top – Osa Oct 22 '12 at 17:53
  • yes, you can use the variable $last to check that. $last contains the number of lines in the file. So if your max is 3, you can use, if($last == 3) { //delete the line and add new line} else { //add new line } – Abhishek Saha Oct 22 '12 at 17:56
  • Nope, not really.. i didnt get it, i got the correct answer though, thanks – Osa Oct 22 '12 at 20:36
0

Modified from Baba's answer; this code will write the new line at the beginning of the file and will errase the last one to keep always 3 lines.

<?php
function addNew($fileName, $line, $max) {
    // Remove Empty Spaces
    $file = array_filter(array_map("trim", file($fileName)));

    // Make Sure you always have maximum number of lines
    $file = array_slice($file, 0, --$max);

    // Remove any extra line and adding the new line
    count($file) >= $max and array_unshift($file, $line);

// Save Result
file_put_contents($fileName, implode(PHP_EOL, array_filter($file)));
}

// Number of lines
$max = 3;
// The file must exist with at least 2 lines on it
$file = "log.txt";
addNew($file, "New Line at : " . time(), $max);

?>
Community
  • 1
  • 1
cronos
  • 536
  • 2
  • 8
  • 20