Thursday, June 11, 2026
HomeLanguagesPHP | SplFileInfo openFile() Function

PHP | SplFileInfo openFile() Function

The SplFileInfo::openFile() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to gets an SplFileObject object for the file.

Syntax:

bool SplFileInfo::openFile( $mode, $path, $context)

Parameters: This function accept three parameters as mention above and describe below.

  • $mode: Specify the mode of opening file.
  • $path: If set it to TRUE, the filename is searched within path.
  • $context: Specifies the manual path for description.

Return values: This function returns opened file as an SplFileObject object.

Note: make sure the file is readable or writable.

Below Program illustrates the SplFileInfo::openFile() function in PHP:

Program:




<?php
   
// PHP Program to illustrate 
// Splfileinfo::openFile function
   
// Make sure file is writable 
$file = new SplFileInfo("gfg.txt");
   
$obj = $file->openFile('a');
   
$obj->fwrite("Append...neveropen to the file...");
   
?>


Output:
Before Appending the file:
openfile

After Appending the file:
openfile

Reference: http://php.net/manual/en/splfileinfo.openfile.php

RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS