The SplFileObject class

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

简介

SplFileObject类为文件提供了一个面向对象接口.

类摘要

SplFileObject extends SplFileInfo implements RecursiveIterator , SeekableIterator {
/* 常量 */
const integer DROP_NEW_LINE = 1 ;
const integer READ_AHEAD = 2 ;
const integer SKIP_EMPTY = 4 ;
const integer READ_CSV = 8 ;
/* 方法 */
public current ( ) : string|array|false
public eof ( ) : bool
public fflush ( ) : bool
public fgetc ( ) : string|false
public fgetcsv ( string $separator = "," , string $enclosure = "\"" , string $escape = "\\" ) : array|false
public fgets ( ) : string
public fgetss ( string $allowable_tags = ? ) : string
public flock ( int $operation , int &$wouldBlock = null ) : bool
public fpassthru ( ) : int
public fputcsv ( array $fields , string $separator = "," , string $enclosure = "\"" , string $escape = "\\" , string $eol = "\n" ) : int|false
public fread ( int $length ) : string|false
public fscanf ( string $format , mixed &...$vars ) : array|int|null
public fseek ( int $offset , int $whence = SEEK_SET ) : int
public fstat ( ) : array
public ftell ( ) : int|false
public ftruncate ( int $size ) : bool
public fwrite ( string $data , int $length = 0 ) : int|false
public getChildren ( ) : RecursiveIterator|null
public getCsvControl ( ) : array
public getFlags ( ) : int
public getMaxLineLen ( ) : int
public hasChildren ( ) : bool
public key ( ) : int
public next ( ) : void
public rewind ( ) : void
public seek ( int $line ) : void
public setCsvControl ( string $separator = "," , string $enclosure = "\"" , string $escape = "\\" ) : void
public setFlags ( int $flags ) : void
public setMaxLineLen ( int $maxLength ) : void
public valid ( ) : bool
/* 继承的方法 */
public SplFileInfo::getATime ( ) : int|false
public SplFileInfo::getBasename ( string $suffix = "" ) : string
public SplFileInfo::getCTime ( ) : int
public SplFileInfo::getExtension ( ) : string
public SplFileInfo::getFileInfo ( string|null $class = null ) : SplFileInfo
public SplFileInfo::getFilename ( ) : string
public SplFileInfo::getGroup ( ) : int|false
public SplFileInfo::getInode ( ) : int|false
public SplFileInfo::getLinkTarget ( ) : string|false
public SplFileInfo::getMTime ( ) : int|false
public SplFileInfo::getOwner ( ) : int|false
public SplFileInfo::getPath ( ) : string
public SplFileInfo::getPathInfo ( string|null $class = null ) : SplFileInfo|null
public SplFileInfo::getPathname ( ) : string
public SplFileInfo::getPerms ( ) : int|false
public SplFileInfo::getRealPath ( ) : string|false
public SplFileInfo::getSize ( ) : int|false
public SplFileInfo::getType ( ) : string|false
public SplFileInfo::isDir ( ) : bool
public SplFileInfo::isExecutable ( ) : bool
public SplFileInfo::isFile ( ) : bool
public SplFileInfo::isLink ( ) : bool
public SplFileInfo::isReadable ( ) : bool
public SplFileInfo::isWritable ( ) : bool
public SplFileInfo::openFile ( string $mode = "r" , bool $useIncludePath = false , resource|null $context = null ) : SplFileObject
public SplFileInfo::setFileClass ( string $class = SplFileObject::class ) : void
public SplFileInfo::setInfoClass ( string $class = SplFileInfo::class ) : void
public SplFileInfo::__toString ( ) : string
}

预定义常量

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.

更新日志

版本 说明
5.3.9 SplFileObject::SKIP_EMPTY value changed to 4. Previously, value was 6.

Table of Contents

User Contributed Notes

rlazarotto15+dont+spam+me at gmail dot com 04-Feb-2021 06:00
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).
marcus at synchromedia dot co dot uk 16-Aug-2014 08:35
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);
Lars Gyrup Brink Nielsen 05-Sep-2013 01:46
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!
---------------------------------------------------------------------
PHP8中文手册 站长在线 整理 版权归PHP文档组所有