Susina Utils

File extends SplFileInfo
in package

SplFileinfo class supporting relative paths and other useful methods for files manipulation.

Tags
author

Cristiano Cinotti cristianocinotti@gmail.com

license

MIT

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
final

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
throws
RuntimeException

If the file does not exist, it's not writable or an error occurs while writing it.

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
throws
RuntimeException

If the file does not exist, it's not readable or an error occurs while reading it.

Return values
string

getRelativePath()

Get the relative path of the file.

public getRelativePath() : string
Return values
string

getRelativePathname()

Get the relative path and file name.

public getRelativePathname() : string
Return values
string

isAbsolute()

Try to determine if a path is absolute.

public isAbsolute() : bool

The method follows these steps:

  1. If the path contains a colon, it is considered absolute (Windows).
  2. If the realpath of the path is the same as the path, it is considered absolute.
  3. If the path starts with a slash, it is considered absolute (Unix).
  4. If the path starts with a backslash, it is considered absolute (Windows UNC).
Return values
bool

makeRelativeTo()

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
File

putContents()

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
throws
RuntimeException

If the file does not exist, it's not writable or an error occurs while writing it.

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
protected
throws
RuntimeException

If the operation is not permitted or an error occurs while performing it.

Return values
string —

The content of the file for 'get' operation, or an empty string for 'put' and 'append' operations.

On this page

Search results