Osa
Osa

Reputation: 1990

PHP file write - max lines

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

Upvotes: 2

Views: 5326

Answers (4)

cronos
cronos

Reputation: 568

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

?>

Upvotes: 0

user428517
user428517

Reputation: 4193

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

Upvotes: 1

Baba
Baba

Reputation: 95101

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

Upvotes: 4

Abhishek Saha
Abhishek Saha

Reputation: 2564

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

?>

Upvotes: 0

Related Questions