Library
Class

Library\Helper\File

class File

File helper

As for all helpers, all methods are statics.

For convenience, the best practice is to use:

use Library\Helper\File as FileHelper;

Properties

static $REPLACEMENT_FILENAMES_CHARS List of characters replaced by a space in a file name to build a human readable string
static $FILESIZE_ORDERED_UNITS List of units to build the size field, ordered by 1024 operator on original size

Methods

static string getUniqFilename(string $filename = '', string $dir = null, boolean $force_file = true, string $extension = 'txt')

Returns a filename or directory that does not exist in the destination

static string formatFilename(string $filename = '', boolean $lowercase = false, string $delimiter = '-')

Formatting file names

static null|string getExtension(string $filename = '', bool $dot = false)

Returns the extension of a file name

static string getHumanReadableFilename(string $filename = '')

Render a human readable string from a file name

static int getTransformedFilesize(float|int $size, int $round = 3, string $dec_delimiter = ',')

Returns a formatted file size in bytes or derived unit

static bool touch(string $file_path = null, array $logs = array())

Create an empty file or touch an existing file

static bool remove(string $file_path = null, array $logs = array())

Remove a file if it exists

static bool copy(string $file_path = null, string $target_path = null, bool $force = false, array $logs = array())

Copy file $file_path if it exists to $target_path

static bool write(string $file_path = null, string $content, string $type = 'a', bool $force = false, array $logs = array())

Write a content in a file

Details

at line 51
static public string getUniqFilename(string $filename = '', string $dir = null, boolean $force_file = true, string $extension = 'txt')

Returns a filename or directory that does not exist in the destination

Parameters

string $filename The name of the file or folder you want to create
string $dir The destination directory
boolean $force_file Should we force the creation of a file, adding an extension? (TRUE by default)
string $extension The extension to add in the case of a file

Return Value

string The filename or directory, with a possible addition to be sure that it does not exist in the destination directory

at line 84
static public string formatFilename(string $filename = '', boolean $lowercase = false, string $delimiter = '-')

Formatting file names

Parameters

string $filename The filename to format
boolean $lowercase Should we return the name un lowercase (FALSE by default)
string $delimiter The delimiter used for special chars substitution

Return Value

string A filename valid on (almost) all systems

at line 123
static public null|string getExtension(string $filename = '', bool $dot = false)

Returns the extension of a file name

It basically returns everything after last dot. No validation is done.

Parameters

string $filename The file_name to work on
bool $dot

Return Value

null|string The extension if found, null otherwise

at line 146
static public string getHumanReadableFilename(string $filename = '')

Render a human readable string from a file name

The original file name is rebuilt striping the extension and a set of commonly used separator characters in file or directories names.

Parameters

string $filename The file_name to work on

Return Value

string The resulting human readable file name

at line 174
static public int getTransformedFilesize(float|int $size, int $round = 3, string $dec_delimiter = ',')

Returns a formatted file size in bytes or derived unit

This will return the size received transforming it to be readable, with the appropriate unit chosen in self::$FILESIZE_ORDERED_UNITS.

Parameters

float|int $size Refer to the size (in standard format given by the stat() function)
int $round The number of decimal places (default is 3)
string $dec_delimiter The decimal separator (default is a comma)

Return Value

int

at line 204
static public bool touch(string $file_path = null, array $logs = array())

Create an empty file or touch an existing file

Parameters

string $file_path
array $logs Logs registry passed by reference

Return Value

bool

at line 231
static public bool remove(string $file_path = null, array $logs = array())

Remove a file if it exists

Parameters

string $file_path
array $logs Logs registry passed by reference

Return Value

bool

at line 258
static public bool copy(string $file_path = null, string $target_path = null, bool $force = false, array $logs = array())

Copy file $file_path if it exists to $target_path

Parameters

string $file_path
string $target_path
bool $force
array $logs Logs registry passed by reference

Return Value

bool

at line 295
static public bool write(string $file_path = null, string $content, string $type = 'a', bool $force = false, array $logs = array())

Write a content in a file

Parameters

string $file_path
string $content
string $type
bool $force
array $logs

Return Value

bool