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 8513517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:35:36+00:00 2026-06-11T04:35:36+00:00

I have a huge log file ( around 1,000,000 lines ). I would like

  • 0

I have a huge log file ( around 1,000,000 lines ). I would like to obtain the last line and remove it from the file using PHP. What is the quickest way to do so?

I tried:

$logfile = escapeshellarg("/path/to/logfile");
$lastline = `tail -n 1 "$logfile"`; // obtained the last line

Is the above approach efficient enough? and how to remove the last line from the file?

From Jon’s answer below, here are the codes :

$buffer_size = 1000;
$fh = fopen("/path/to/logfile", "r+");
fseek($fh, -$buffer_size, SEEK_END);
$content = fgets($fh, 100);
while(strrpos($content, PHP_EOL) != false) {
  fseek($fh, -$buffer_size); // move backward for extra -1000
  $content = fgets($fh, $buffer_size);
}
$pos_last_eol = strrpos($content, PHP_EOL);
fseek($fh, $pos_last_eol); // seek to that position
ftruncate($fh, ftell($fh));
fclose($fh);
  • 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-11T04:35:38+00:00Added an answer on June 11, 2026 at 4:35 am

    The fastest way to obtain and remove the last line from a big file is:

    1. Open the file for writing
    2. Seek to the end
    3. Seek some arbitrary buffer length backwards (let’s say 1K) and read data to fill the buffer
    4. Search the buffer backwards with something like strrpos until you find an end-of-line marker¹
    5. If you do not find an EOL, go to step 3 and repeat
    6. If you do find an EOL, you know the file offset at which it occurs based on the position in the buffer and the offset at which the buffer was read from
    7. Obtain the last line by seeking to that offset and reading until end of file²
    8. Call ftruncate to cut off the part of the file beginning with the end of line found

    ¹ Supporting all of \n, \r, \r\n is going to complicate things a
    little; especially for the latter, it could always happen to span
    across two buffers so you ‘d have to explicitly watch out for that.

    ² This is not strictly necessary because all the data you are going to
    read has already passed through the buffer, so you could have kept a
    copy and saved the cost of this operation. In practice though the last
    line is not going to be too long so it’s more convenient to just
    re-read the whole thing (C runtime and/or OS filesystem cache will probably make this
    stupidly fast anyway).

    This is what any program would have to do. If you decide to “cheat” by offloading the first seven steps to an external utility like tail you can remove the line from the file with one call to ftruncate, but: be careful when calculating the offset at which to truncate if you do not wish to leave trailing end-of-line character(s) in the file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a huge log file where there are about 15 million lines. I
I have a really huge log file from which i need to search stuff.
I have some HUGE log files (50Mb; ~500K lines) I need to start filtering
I have a huge checkbox list with different levels and I would like to
I have a huge log file containing log messages prefixed with timestamp. The timestamp
Hi I'm using EmEditor because I have huge log files and it handles them
I have created a code using jackrabbit-standalone-2.4.2.jar and i am getting jackrabbit.log_IS_UNDEFINED.log file in
I have huge 1 GB log file. As I know, it shows errors in
I have a huge set of log lines and I need to parse each
I have a SQLite database that contains a huge set of log messages. I

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.