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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:19:40+00:00 2026-05-18T20:19:40+00:00

I have a function that strips out lines from files. I’m handling with large

  • 0

I have a function that strips out lines from files. I’m handling with large files(more than 100Mb). I have the PHP Memory with 256MB but the function that handles with the strip out of lines blows up with a 100MB CSV File.

What the function must do is this:

Originally I have the CSV like:

Copyright (c) 2007 MaxMind LLC. All
Rights Reserved.
locId,country,region,city,postalCode,latitude,longitude,metroCode,areaCode
1,”O1″,””,””,””,0.0000,0.0000,,
2,”AP”,””,””,””,35.0000,105.0000,,
3,”EU”,””,””,””,47.0000,8.0000,,
4,”AD”,””,””,””,42.5000,1.5000,,
5,”AE”,””,””,””,24.0000,54.0000,,
6,”AF”,””,””,””,33.0000,65.0000,,
7,”AG”,””,””,””,17.0500,-61.8000,,
8,”AI”,””,””,””,18.2500,-63.1667,,
9,”AL”,””,””,””,41.0000,20.0000,,

When I pass the CSV file to this function I got:

locId,country,region,city,postalCode,latitude,longitude,metroCode,areaCode
1,”O1″,””,””,””,0.0000,0.0000,,
2,”AP”,””,””,””,35.0000,105.0000,,
3,”EU”,””,””,””,47.0000,8.0000,,
4,”AD”,””,””,””,42.5000,1.5000,,
5,”AE”,””,””,””,24.0000,54.0000,,
6,”AF”,””,””,””,33.0000,65.0000,,
7,”AG”,””,””,””,17.0500,-61.8000,,
8,”AI”,””,””,””,18.2500,-63.1667,,
9,”AL”,””,””,””,41.0000,20.0000,,

It only strips out the first line, nothing more. The problem is the performance of this function with large files, it blows up the memory.

The function is:

 public function deleteLine($line_no, $csvFileName) {

  // this function strips a specific line from a file
  // if a line is stripped, functions returns True else false
  //
  // e.g.
  // deleteLine(-1, xyz.csv); // strip last line
  // deleteLine(1, xyz.csv); // strip first line

  // Assigna o nome do ficheiro
  $filename = $csvFileName;

  $strip_return=FALSE;

  $data=file($filename);
  $pipe=fopen($filename,'w');
  $size=count($data);

  if($line_no==-1) $skip=$size-1;
  else $skip=$line_no-1;

  for($line=0;$line<$size;$line++)
   if($line!=$skip)
    fputs($pipe,$data[$line]);
   else
    $strip_return=TRUE;

  return $strip_return;
 }

It is possible to refactor this function to not blow up with the 256MB PHP Memory?

Give me some clues.

Best Regards,

  • 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-05-18T20:19:40+00:00Added an answer on May 18, 2026 at 8:19 pm

    The problem for your blowout is the file function that brings the entire file in memory. To overcome this you need to read the file line by line, write all but the line to be deleted to a temporary file and finally rename the temporary file.

    public function deleteLine($line_no, $csvFileName) {
    
            // get a temp file name in current working directory..you can use
            // any other directory say /tmp
            $tmpFileName = tempnam(".", "csv");
    
            $strip_return=FALSE;
    
            // open input file for reading.
            $readFD=fopen($csvFileName,'r');
    
            // temp file for writing.
            $writeFD=fopen($tmpFileName,'w');
    
            // check for fopen errors.
    
            if($line_no==-1) {
                    $skip=$size-1;
            } else {
                    $skip=$line_no-1;
            }
    
            $line = 0;
    
            // read lines from input file one by one.
            // write all lines except the line to be deleted.
            while (($buffer = fgets($readFD)) !== false) {
                    if($line!=$skip)
                            fputs($writeFD,$buffer);
                    else
                            $strip_return=TRUE;
                    $line++;
            }
    
            // rename temp file to input file.    
            rename($tmpFileName,$csvFileName);
    
            return $strip_return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few divs that are created using PHP. The anchor within the
I have a form that allows the user to add information an their leisure.
I have a static library, Foo, that is used by a shared library, Bar.
I’m currently working on a reporting library as part of a large project. It
I have a caption for a GUI control, and I want to convert it
I am working on a PHP document which has jQuery code in it. I
Given a DIV with this general structure (class=post, from an extracted message board post)
A seriously flawed and retarded piece of software which goes by the name of
I am using wordpress as a CMS and trying to allow user fields to
this is my first question.. be gentle :) I am part of the development

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.