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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:34:17+00:00 2026-06-13T21:34:17+00:00

[EDITED OP OUT HERE IS THE SHORT VERSION] Looping through a file and reading

  • 0

[EDITED OP OUT HERE IS THE SHORT VERSION]

Looping through a file and reading contents, then writing causes the function to fail. It appeared to be a memory issue. This is the three versions I tried.

First tried this:

$file = new SplFileObject($this->getDirectoryPath() . $this->getFileName(), "a+");
$file->setFlags(SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY);

if ($this->exists()) {
    foreach ($file as $line) {
        $tempArray = unserialize($line);
        if ($tempArray['Key'] == $arrayOfData['Key']) {
            foreach ($totalsToBeAdded as $key) {
                $arrayOfData[$key] += $tempArray[$key];
            }
        }
    }
}

$tempString = serialize($arrayOfData);

$file->fwrite("$tempString\r\n");

$this->numLines++;

Then I tried this:

$file = new SplFileObject($this->getDirectoryPath() . $this->getFileName(), "a+");
$file->setFlags(SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY);

if ($this->exists()) {
    while (!$file->eof()) {
        $tempArray = unserialize($file->current());
        if ($tempArray['PartNumber'] == $arrayOfData['PartNumber']) {
            foreach ($totalsToBeAdded as $key) {
                $arrayOfData[$key] += $tempArray[$key];
            }
        }

        $file->next();
    }
}

$tempString = serialize($arrayOfData);

$file->fwrite("$tempString\r\n");

$this->numLines++;

And finally I abandoned SplFileObject and just went with normal fopen etc:

$handle = fopen($this->getDirectoryPath() . $this->getFileName(), "a+");

if ($this->exists()) {
    while (false !== ($line = fgets($handle))) {
        $tempArray = unserialize(trim($line));
        if ($tempArray['Key'] == $arrayOfData['Key']) {
            foreach ($totalsToBeAdded as $key) {
                $arrayOfData[$key] += $tempArray[$key];
            }
        }
    }
}

$tempString = serialize($arrayOfData);
fwrite($handle, "$tempString\r\n");
fclose($handle);
$this->numLines++;

EDIT FOR MORE INFO:

I was curious if the underlying code of PHP used array for the iterators when stepping line by line through a file, which could kill it.

Also the file does begin building, I can watch it write till it gets to about 500-600k then it dies.

The final file size will be around 10mb.

One final update:

This works (notice lack of openning and reading file):

public function writeUnique($arrayOfData, $totalsToBeAdded) {  
        $tempArray = array();

        $handle = fopen($this->fullPath, "a+");

        $tempString = serialize($arrayOfData);
        fwrite($handle, "$tempString\r\n");
        fclose($handle);
        $this->numLines++;
}

While this breaks (notice ALL that is being done is looping through the whole file THEN writing to the file):

public function writeUnique($arrayOfData, $totalsToBeAdded) {  
        $tempArray = array();

        $handle = fopen($this->fullPath, "a+");

        if ($this->exists()) {
            while (false !== ($line = fgets($handle))) {

            }
        }

        $tempString = serialize($arrayOfData);
        fwrite($handle, "$tempString\r\n");
        fclose($handle);
        $this->numLines++;
}

UPDATE NUMBER THREE:

I have now tested this:

public function writeUnique($arrayOfData, $totalsToBeAdded) {

    $handle = fopen($this->fullPath, "a+");

    if ($this->exists()) {
        while (false !== ($line = fgets($handle))) {

        }
    }

    $tempString = serialize($arrayOfData);
//        fwrite($handle, "$tempString\r\n"); Commented out the writing.
    fclose($handle);
    $this->numLines++;
}

This worked. No failure, memory error or other wise.

So, it appears that it is either a problem with iterations of rereading the same lines of a large file, OR the write portion of the function is in some way steping on the toes of the read function.. which honestly doesn’t make sense. I know everyone was thinking it has something to do with my arrays. But I’ve preaty much taken out ALL my logic and I’m just trying to read/write a large file.

  • 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-13T21:34:19+00:00Added an answer on June 13, 2026 at 9:34 pm

    So I finally just broke down and did the math to figure out how many loops I’m requiring php to complete on this file, and the number is 8,788,338,000,000 times.

    This in turn caused the PHP to time out. To keep it from timing out this line of code needed to be added.

    set_time_limit(0); // ignore php timeout
    

    Now the temp files can all be read and parsed line by line. However, on large files (10 mb +), the time to complete the function is well over an hour so far (it’s still running as I can see the temp file growing larger).

    I have come to the conlusion that if speed is of the essence, then it will probably be better to store LARGE data sets into a temporary SQL table. This previously wasn’t an option for me, but now I’m forcing the issue with the powers that be to allow it. Worst case senerio this will atleast allow it to run.

    BE WARNED: THIS WILL ALLOW AN INFINITE LOOP TO RUN FOREVER AND POSSIBLY KILL THE SERVER. MAKE SURE YOU KNOW HOW TO KILL THE PROCESS THROUGH UNIX BEFORE ATTEMPTING.

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

Sidebar

Related Questions

(Edited to avoid leading down the wrong road) Before giving details, very short version:
I have a branch checked out in Tortoise 1.4.2, edited it in 1.6.2 and
Using vi I started editing a html file and I accidentally exited out of
EDITED * * Hello I am new to PHP and trying to figure out
When a user logs in, here is my user function: public function login($user) {
Odd problem, trying to figure out what is going on here with my fresh
Edited after getting answers Some excellent answers here. I like Josh's because it is
Here is an example of polymorphism from http://www.cplusplus.com/doc/tutorial/polymorphism.html (edited for readability): // abstract base
I was trying out the validity of private access specifier in C++. Here goes:
Edited at the request of commenters. I hope this is compliant. First post! Trying

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.