php8.5
Home/ Manual/ spl / splfileinfo/ SplFileInfo::openFile

SplFileInfo::openFile

PHP function Edit on GitHub ✎

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

Gets an SplFileObject object for the file

Description

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

Creates an SplFileObject object 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

Use_include_path

context

Context

Return Values

The opened file as an SplFileObject object.

Errors/Exceptions

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

Changelog

VersionDescription
8.0.0context is now nullable.

Examples

SplFileInfo::openFile example

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

if ($fileinfo->isWritable()) {

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

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

See Also

Source: reference/spl/splfileinfo/openfile.xml · from the official PHP manual (php/doc-en)