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

  • Home
  • SEARCH
  • 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 7740447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:46:50+00:00 2026-06-01T08:46:50+00:00

Lets say I have a modestly sized text file (~850kb, 10,000+ lines) And I

  • 0

Lets say I have a modestly sized text file (~850kb, 10,000+ lines)

And I want to replace a particular line (or several) spread out amongst the file.

Current methods for doing this include re-writing the whole file. The current method I use is read through the entire file line by line, writing to a .tmp file, and once I am done, I rename() the tmp file to the original source file.

It works, but it is slow. And of course, as the file grows, so will execution times.

Is there another way (using PHP) to get the job done without having to rewrite the entire file every time a line or two need to be replaced or removed?

Thanks! I looked around and could not find an answer to this on stackoverflow.

  • 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-01T08:46:51+00:00Added an answer on June 1, 2026 at 8:46 am

    If the replacement is EXACTLY the same size as the original line, then you can simply fwrite() at that location in the file and all’s well. But if it’s a different length (shorter OR longer), you will have to rewrite the portion of the file that comes AFTER the replacement.

    There is no way around this. Shorter ‘new’ lines will leave a gap in the file, and longer ‘new’ lines would overwrite the first part of the next line.

    Basically, you’re asking if it’s possible to insert a piece of wood in the middle of another board without having to move the original board around.

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

Sidebar

Related Questions

Lets say I have a .c file with some methods, and I create a
Lets say I have these columns uniqueID|Money|Quantity|MoneyOrder|QuantityOrder 1|23|12|| 2|11|9|| 3|99|100|| What I want to
Lets say I have several sets of options in Javascript var color = [red,
Lets say I have a file with a .quote extension. I'd like the web
Lets say I have heap allocated A* , which I want to pass as
Lets say I have a list: listOfStuff =([a,b], [c,d], [e,f], [f,g]) What I want
Lets say I have the following: foo = ('animal', 'vegetable', 'mineral') I want to
Lets say I have an array of structs and I want to delete an
Lets say I have a URL as follows: http://www.testing.com/?id=1234 I want to take that
Lets say I have several web sites on my web server, all as applications

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.