SplFileObject クラス

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

はじめに

SplFileObject クラスはファイルのためのオブジェクト指向のインターフェイスを提供します。

クラス概要

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

定義済み定数

SplFileObject::DROP_NEW_LINE

行末の改行を読み飛ばします。

SplFileObject::READ_AHEAD

先読み/巻き戻しで読み出します。

SplFileObject::SKIP_EMPTY

ファイルの空行を読み飛ばします。期待通りに動作させるには、READ_AHEAD フラグを有効にしないといけません。

SplFileObject::READ_CSV

CSV 列として行を読み込みます。

目次

add a note

User Contributed Notes 4 notes

up
90
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!
---------------------------------------------------------------------
up
4
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);
up
1
contact at trimal dot in
2 years ago
with php 8.3, with or without SplFileObject::DROP_NEW_LINE, you get an array with empty values at the end.
up
-2
rlazarotto15+dont+spam+me at gmail dot com
5 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