Reputation: 9418
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.
Upvotes: 0
Views: 2039
Reputation: 51950
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));
Upvotes: 2
Reputation: 16269
If what you need is the number of lines inside the file:
<?php
$path = tempnam(sys_get_temp_dir(), '').'.txt';
$fileInfo = new SplFileInfo($path);
$fileObject = $fileInfo->openFile('a+');
$fileObject->fwrite("Foo".PHP_EOL);
$fileObject->fwrite("Bar".PHP_EOL);
echo count(file($path)); // outputs 2
?>
This is your code above, but without entering into infinite loops due to the file pointer:
<?php
$path = tempnam(sys_get_temp_dir(), '').'.txt';
$fileInfo = new SplFileInfo($path);
$fileObject = $fileInfo->openFile('a+');
$fileObject->fwrite("Foo".PHP_EOL);
$fileObject->fwrite("Bar");
foreach($fileObject as $line_num => $line) {
echo 'Line: '.$line_num.' "'.$line.'"'."<br/>";
}
echo 'Total Lines:' . $fileObject->key();
?>
Outputs
Line: 0 "Foo "
Line: 1 "Bar"
Total Lines:2
The logic applied was a bit off. I simplified the code:
<?php
// set path to tmp with random file name
echo $path = tempnam(sys_get_temp_dir(), '').'.txt';
echo "<br/>";
// new object
$fileInfo = new \SplFileInfo($path);
// open to write
$fileObject = $fileInfo->openFile('a');
// write two lines
$fileObject->fwrite("Foo".PHP_EOL);
$fileObject->fwrite("Bar".PHP_EOL);
// open to read
$fileObject2 = $fileInfo->openFile('r');
// output contents
echo "File Exists: " .file_exists($path);
echo "<br/>";
echo "File Contents: " . file_get_contents($path);
echo "<br/>";
// foreach line get line number and line contents
foreach($fileObject2 as $line_num => $line) {
echo 'Line: '.$line_num;
echo ' With: "'.$line.'" is the end? '.($fileObject2->eof()?'yes':'no')."<br>";
}
?>
Outputs:
/tmp/EAdklY.txt
File Exists: 1
File Contents: Foo Bar
Line: 0 With: "Foo " is the end? no
Line: 1 With: "Bar " is the end? no
Line: 2 With: "" is the end? yes
Upvotes: 1
Reputation: 12189
While it may seem counter intuitive, with PHP 5.3.9, this:
<?php
$f = new SplFileObject('test.txt', 'r');
while (!$f->eof()) {
$f->next();
}
while be an infinite loop, and never exit.
The following will exit, when the end of the file is reached:
<?php
$f = new SplFileObject('test.txt', 'r');
while (!$f->eof()) {
$f->current();
}
So:
$i = 0;
$fileObject2->rewind();
while (!$fileObject2->eof()) {
var_dump($fileObject2->eof());
var_dump($i++);
$fileObject2->next();
}
should be rewritten as:
$fileObject2->rewind();
while (!$fileObject2->eof()) {
$fileObject2->current();
}
$i = $fileObject2->key();
Upvotes: 0