SplFileObject 类
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
简介
SplFileObject 类为文件提供了一个面向对象接口。
类摘要
/* 常量 */
/* 方法 */
public __construct(
string
string
bool
?resource
)
string
$filename
,string
$mode
= "r",bool
$useIncludePath
= false
,?resource
$context
= null
)
public fgetcsv(string
$separator
= ",", string $enclosure
= "\"", string $escape
= "\\"): array|falsepublic fputcsv(
array
string
string
string
string
): int|false
array
$fields
,string
$separator
= ",",string
$enclosure
= "\"",string
$escape
= "\\",string
$eol
= "\n"): int|false
public setCsvControl(string
$separator
= ",", string $enclosure
= "\"", string $escape
= "\\"): void/* 继承的方法 */
public SplFileInfo::openFile(string
}$mode
= "r", bool $useIncludePath
= false
, ?resource $context
= null
): SplFileObject预定义常量
SplFileObject::DROP_NEW_LINE
-
删除行尾的换行符。
SplFileObject::READ_AHEAD
-
使用 rewind 或 next 方法时,从文件中读取一行数据。
SplFileObject::SKIP_EMPTY
-
跳过文件中的空白行。这需要启用
READ_AHEAD
标志,以达到预期的效果。 SplFileObject::READ_CSV
-
以 CSV 行的形式读取。
目录
- SplFileObject::__construct — Construct a new file object
- SplFileObject::current — Retrieve current line of file
- SplFileObject::eof — Reached end of file
- SplFileObject::fflush — Flushes the output to the file
- SplFileObject::fgetc — Gets character from file
- SplFileObject::fgetcsv — Gets line from file and parse as CSV fields
- SplFileObject::fgets — Gets line from file
- SplFileObject::fgetss — Gets line from file and strip HTML tags
- SplFileObject::flock — Portable file locking
- SplFileObject::fpassthru — Output all remaining data on a file pointer
- SplFileObject::fputcsv — Write a field array as a CSV line
- SplFileObject::fread — Read from file
- SplFileObject::fscanf — Parses input from file according to a format
- SplFileObject::fseek — Seek to a position
- SplFileObject::fstat — Gets information about the file
- SplFileObject::ftell — Return current file position
- SplFileObject::ftruncate — Truncates the file to a given length
- SplFileObject::fwrite — Write to file
- SplFileObject::getChildren — No purpose
- SplFileObject::getCsvControl — Get the delimiter, enclosure and escape character for CSV
- SplFileObject::getCurrentLine — Alias of SplFileObject::fgets
- SplFileObject::getFlags — Gets flags for the SplFileObject
- SplFileObject::getMaxLineLen — Get maximum line length
- SplFileObject::hasChildren — SplFileObject does not have children
- SplFileObject::key — Get line number
- SplFileObject::next — Read next line
- SplFileObject::rewind — Rewind the file to the first line
- SplFileObject::seek — Seek to specified line
- SplFileObject::setCsvControl — Set the delimiter, enclosure and escape character for CSV
- SplFileObject::setFlags — Sets flags for the SplFileObject
- SplFileObject::setMaxLineLen — Set maximum line length
- SplFileObject::__toString — Alias of SplFileObject::fgets
- SplFileObject::valid — Not at EOF

User Contributed Notes 3 notes
Lars Gyrup Brink Nielsen ¶
8 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 ¶
7 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);
rlazarotto15+dont+spam+me at gmail dot com ¶
1 year 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).