File
extends SplFileInfo
in package
SplFileinfo class supporting relative paths and other useful methods for files manipulation.
Tags
Table of Contents
Properties
- $relativePath : string
Methods
- __construct() : mixed
- appendContents() : void
- Append the given string to the end of the file.
- getContents() : string
- Read the contents of the file.
- getRelativePath() : string
- Get the relative path of the file.
- getRelativePathname() : string
- Get the relative path and file name.
- isAbsolute() : bool
- Try to determine if a path is absolute.
- makeRelativeTo() : File
- Make the current path relative to the one passed as argument.
- putContents() : void
- Write the given string into the file.
- fileOperation() : string
- Perform an operation on the file.
Properties
$relativePath
protected
string
$relativePath
= ''
The relative path.
Methods
__construct()
public
final __construct(string $filename[, string $relativePath = '' ]) : mixed
Parameters
- $filename : string
-
The file name. It can be relative to the path passed as second argument.
- $relativePath : string = ''
-
The relative path.
Tags
appendContents()
Append the given string to the end of the file.
public
appendContents(string $content) : void
This method append the content to the file. If the file does not exist, it will be created.
Parameters
- $content : string
-
The content to append.
Tags
getContents()
Read the contents of the file.
public
getContents() : string
This method reads the full content of the file. To read only a certain number
of bytes, please use the native \SplObject::fread() instead.
Example 1: read the full content of the file:
<?php
$file = new File('myfile.txt');
$content = $file->getContents();
Example 2: read only the first 100 bytes of the file:
<?php
$file = new File('myfile.txt');
$fileObject = $file->openFile('r');
$content = $fileObject->fread(100);
// or in a single line
$content = new File('myfile.txt')->openFile('r')->fread(100);
Tags
Return values
stringgetRelativePath()
Get the relative path of the file.
public
getRelativePath() : string
Return values
stringgetRelativePathname()
Get the relative path and file name.
public
getRelativePathname() : string
Return values
stringisAbsolute()
Try to determine if a path is absolute.
public
isAbsolute() : bool
The method follows these steps:
- If the path contains a colon, it is considered absolute (Windows).
- If the realpath of the path is the same as the path, it is considered absolute.
- If the path starts with a slash, it is considered absolute (Unix).
- If the path starts with a backslash, it is considered absolute (Windows UNC).
Return values
boolmakeRelativeTo()
Make the current path relative to the one passed as argument.
public
makeRelativeTo(string $path) : File
It returns another File object with relative path.
Parameters
- $path : string
Return values
FileputContents()
Write the given string into the file.
public
putContents(string $content) : void
This method overwrite the content of the file and writes the complete string.
To append content to the file, please use the appendContents() method instead.
If the file does not exist, it will be created.
Parameters
- $content : string
Tags
fileOperation()
Perform an operation on the file.
protected
fileOperation(string $operation[, string $content = '' ]) : string
Parameters
- $operation : string
-
The operation to perform. It can be 'get', 'put' or 'append'.
- $content : string = ''
-
The content to write or append. It is only used for 'put' and 'append' operations.
Tags
Return values
string —The content of the file for 'get' operation, or an empty string for 'put' and 'append' operations.