SplFileInfo::openFile

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

SplFileInfo::openFileGets an SplFileObject object for the file

Description

publicSplFileInfo::openFile(string$mode = "r", bool$useIncludePath = false, ?resource$context = null): SplFileObject

Creates an SplFileObjectobject of the file. This is useful because SplFileObject contains additional methods for manipulating the file whereas SplFileInfo is only useful for gaining information, like whether the file is writable.

Parameters

mode

The mode for opening the file. See the fopen() documentation for descriptions of possible modes. The default is read only.

useIncludePath

When set to true, the filename is also searched for within the include_path

context

Refer to the context section of the manual for a description of contexts.

Return Values

The opened file as an SplFileObjectobject.

Errors/Exceptions

A RuntimeException if the file cannot be opened (e.g. insufficient access rights).

Changelog

VersionDescription
8.0.0context is now nullable.

Examples

Example #1 SplFileInfo::openFile() example

<?php
$fileinfo
= new SplFileInfo('/tmp/foo.txt');

if (
$fileinfo->isWritable()) {

$fileobj = $fileinfo->openFile('a');

$fileobj->fwrite("appended this sample text");
}
?>

See Also

To Top