Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8730593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:03:16+00:00 2026-06-13T09:03:16+00:00

How to put a limit for writing in a file, if it hit the

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:03:17+00:00Added an answer on June 13, 2026 at 9:03 am

    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)));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey, hi i want put limit on object creation means a class can have
Put simply, I want to select all data in two columns from each of
Put value as a field makes sense only if this one represents some object's
Whats the best approach for writing data to a file that will be picked
Is there any way in CSS to put a limit on how far something
Is it possible to put an artificial limit on the amount of memory a
Kindly let me know how can I put the limit on the record in
I've been trying to use a <limit> tag to disable PUT and DELETE calls
I have a trouble solving out this fiddle. When i put limits i am
Put them all in one separate folder structure or along with classes which implements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.