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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:09:57+00:00 2026-06-08T19:09:57+00:00

Why is my PHP script hanging? $path = tempnam(sys_get_temp_dir(), ”).’.txt’; $fileInfo = new \SplFileInfo($path);

  • 0

Why is my PHP script hanging?

$path = tempnam(sys_get_temp_dir(), '').'.txt';
$fileInfo = new \SplFileInfo($path);
$fileObject = $fileInfo->openFile('a');
$fileObject->fwrite("test line\n");
$fileObject2 = $fileInfo->openFile('r');
var_dump(file_exists($path));          // bool(true)
var_dump(file_get_contents($path));    // string(10) "test line
                                       // "
var_dump(iterator_count($fileObject2)); // Hangs on this

If I delete the last line (iterator_count(...) and replace it with this:

$i = 0;
$fileObject2->rewind();
while (!$fileObject2->eof()) {
    var_dump($fileObject2->eof());
    var_dump($i++);
    $fileObject2->next();
}

// Output:
// bool(false)
// int(0)
// bool(false)
// int(1)
// bool(false)
// int(2)
// bool(false)
// int(3)
// bool(false)
// int(4)
// ...

The $fileObject->eof() always returns false so I get an infinite loop.

Why are these things happening? I need to get a line count.

  • 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-08T19:09:58+00:00Added an answer on June 8, 2026 at 7:09 pm

    Why are these things happening?

    You are experiencing a peculiarity in the way that the SplFileObject class is written. Without calling next() and current() methods—using the default (0) flags—the iterator never moves forward.

    The iterator_count() function never calls current(); it checks valid() and calls next() only. Your bespoke loops only call one or other of current() and next().

    This should be considered a bug (whether in PHP itself, or a failure in the documentation) and the following code should (and does not) work as expected. I invite you to report this misbehaviour.

    // NOTE: This currently runs in an infinite loop!
    $file = new SplFileObject(__FILE__);
    var_dump(iterator_count($file));
    

    Workarounds

    One quick sidestep to get things moving is to set the READ_AHEAD flag on the object. This will cause the next() method to read the next available line.

    $file->setFlags(SplFileObject::READ_AHEAD);
    

    If, for any reason, you do not want the read ahead behaviour then you must call both next() and current() yourself.

    Back to the original problem of two SplFileObjects

    The following should now work as you expected, allowing appending to a file and reading its line count.

    <?php
    $info = new SplFileInfo(__FILE__);
    $write = $info->openFile('a');
    $write->fwrite("// new line\n");
    $read = $info->openFile('r');
    $read->setFlags(SplFileObject::READ_AHEAD);
    var_dump(iterator_count($read));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why is my PHP script hanging? $path = tempnam(sys_get_temp_dir(), '').'.txt'; $fileInfo = new \SplFileInfo($path);
PHP script to read in user action requests and parse them to their components.
My PHP script is displaying the wrong time. For eg. the time on my
My PHP script generates a table with rows which can optionaly be edited or
A php script is giving this array (which has been passed through json_encode() )
A php script is building the following (very complicated script that is too long
I am writing a PHP script to upload and re-size 4 images. I have
I made a little PHP script that checks if an email is valid. The
I wrote a PHP script that retrieves values from a MySQL Query. I used
I writing a PHP script program under Linux. In the script, I need call

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.