The SplFileObject class

(PHP 5 >= 5.1.0, PHP 7)

Wstęp

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

Krótki opis klasy

SplFileObject extends SplFileInfo implements RecursiveIterator , SeekableIterator {
/* Stałe */
const integer DROP_NEW_LINE = 1 ;
const integer READ_AHEAD = 2 ;
const integer SKIP_EMPTY = 4 ;
const integer READ_CSV = 8 ;
/* Metody */
public __construct ( string $filename [, string $open_mode = "r" [, bool $use_include_path = FALSE [, resource $context ]]] )
public current ( void ) : string|array
public eof ( void ) : bool
public fflush ( void ) : bool
public fgetc ( void ) : string
public fgetcsv ([ string $delimiter = "," [, string $enclosure = "\"" [, string $escape = "\\" ]]] ) : array
public fgets ( void ) : string
public fgetss ([ string $allowable_tags ] ) : string
public flock ( int $operation [, int &$wouldblock ] ) : bool
public fpassthru ( void ) : int
public fputcsv ( array $fields [, string $delimiter = "," [, string $enclosure = '"' [, string $escape = "\\" ]]] ) : int
public fread ( int $length ) : string
public fscanf ( string $format [, mixed &$... ] ) : mixed
public fseek ( int $offset [, int $whence = SEEK_SET ] ) : int
public fstat ( void ) : array
public ftell ( void ) : int
public ftruncate ( int $size ) : bool
public fwrite ( string $str [, int $length ] ) : int
public getChildren ( void ) : void
public getCsvControl ( void ) : array
public getFlags ( void ) : int
public getMaxLineLen ( void ) : int
public hasChildren ( void ) : bool
public key ( void ) : int
public next ( void ) : void
public rewind ( void ) : void
public seek ( int $line_pos ) : void
public setCsvControl ([ string $delimiter = "," [, string $enclosure = "\"" [, string $escape = "\\" ]]] ) : void
public setFlags ( int $flags ) : void
public setMaxLineLen ( int $max_len ) : void
public valid ( void ) : bool
/* Metody dziedziczone */
public SplFileInfo::getATime ( void ) : int
public SplFileInfo::getBasename ([ string $suffix ] ) : string
public SplFileInfo::getCTime ( void ) : int
public SplFileInfo::getExtension ( void ) : string
public SplFileInfo::getFileInfo ([ string $class_name ] ) : SplFileInfo
public SplFileInfo::getFilename ( void ) : string
public SplFileInfo::getGroup ( void ) : int
public SplFileInfo::getInode ( void ) : int
public SplFileInfo::getLinkTarget ( void ) : string
public SplFileInfo::getMTime ( void ) : int
public SplFileInfo::getOwner ( void ) : int
public SplFileInfo::getPath ( void ) : string
public SplFileInfo::getPathInfo ([ string $class_name ] ) : SplFileInfo
public SplFileInfo::getPathname ( void ) : string
public SplFileInfo::getPerms ( void ) : int
public SplFileInfo::getRealPath ( void ) : string
public SplFileInfo::getSize ( void ) : int
public SplFileInfo::getType ( void ) : string
public SplFileInfo::isDir ( void ) : bool
public SplFileInfo::isExecutable ( void ) : bool
public SplFileInfo::isFile ( void ) : bool
public SplFileInfo::isLink ( void ) : bool
public SplFileInfo::isReadable ( void ) : bool
public SplFileInfo::isWritable ( void ) : bool
public SplFileInfo::openFile ([ string $open_mode = "r" [, bool $use_include_path = FALSE [, resource $context = NULL ]]] ) : SplFileObject
public SplFileInfo::setFileClass ([ string $class_name = "SplFileObject" ] ) : void
public SplFileInfo::setInfoClass ([ string $class_name = "SplFileInfo" ] ) : void
public SplFileInfo::__toString ( void ) : string
}

Stałe predefiniowane

SplFileObject::DROP_NEW_LINE

Drop newlines at the end of a line.

SplFileObject::READ_AHEAD

Read on rewind/next.

SplFileObject::SKIP_EMPTY

Skips empty lines in the file. This requires the READ_AHEAD flag be enabled, to work as expected.

SplFileObject::READ_CSV

Read lines as CSV rows.

Rejestr zmian

Wersja Opis
5.3.9 SplFileObject::SKIP_EMPTY value changed to 4. Previously, value was 6.

Spis treści

add a note add a note

User Contributed Notes 3 notes

up
70
Lars Gyrup Brink Nielsen
10 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!
---------------------------------------------------------------------
up
5
marcus at synchromedia dot co dot uk
9 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);
up
-1
rlazarotto15+dont+spam+me at gmail dot com
3 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
   
$line  $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).
To Top