PHP: SplFileObject - Manual (original) (raw)

The SplFileObject class

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

Introduction

The SplFileObject class offers an object-oriented interface for a file.

Class synopsis

Table of Contents

Found A Problem?

Lars Gyrup Brink Nielsen

12 years ago

Note that this class has a private (and thus, not documented) property that holds the file pointer. Combine this with the fact that there is no method to close the file handle, and you get into situations where you are not able to delete the file with unlink(), etc., because an SplFileObject still has a handle open.

To get around this issue, delete the SplFileObject like this:

---------------------------------------------------------------------
<?php
print "Declaring file object\n";
$file = new SplFileObject('example.txt');

print "Trying to delete file...\n";
unlink('example.txt');

print "Closing file object\n";
$file = null;

print "Deleting file...\n";
unlink('example.txt');

print 'File deleted!';
?>
---------------------------------------------------------------------

which will output:

---------------------------------------------------------------------
Declaring file object 
Trying to delete file... 

Warning: unlink(example.txt): Permission denied in file.php on line 6
Closing file object 
Deleting file... 
File deleted!
---------------------------------------------------------------------

marcus at synchromedia dot co dot uk

11 years ago

If you want to skip blank lines when reading a CSV file, you need *all * the flags:

$file->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE);

contact at trimal dot in

1 year ago

with php 8.3, with or without SplFileObject::DROP_NEW_LINE, you get an array with empty values at the end.

rlazarotto15+dont+spam+me at gmail dot com

4 years ago

Complimenting marcus at synchromedia dot co dot uk comment, you can also do something like this:

<?php

// create a SplFileObject for reading - note that there are no flags
$file = new SplFileObject('/path/to/file', 'r');

// iterate over its contents
while (!$file->eof()) {
    // get the current line
    <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>l</mi><mi>i</mi><mi>n</mi><mi>e</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">line  =  </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">in</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>file->fgets();

    // trim it, and then check if its empty
    if (empty(trim($line))) {
        // skips the current iteration
        continue;
    }
}

While this may seem like a overkill for such thing, it allows you to do some processing with the empty lines that might come (I had to do this mostly because I needed to count empty lines instead of just skipping them). Since it also trims the line before checking if it's empty, you won't get lines composed only of empty spaces (I don't know if the flags also make it trim the content before checking it).