RSI helpers  0.1
RSI helpers
File Class Reference

File and file system helper functions. More...

Static Public Member Functions

static addDirSeparator ($path)
 Add a directory separator to the path (if not present) More...
 
static windows ($filename)
 Normalize a Windows filename. More...
 
static inDir ($path, $filename)
 Check if a filename is within a directory. More...
 
static inBaseDir ($filename)
 Check if a filename is within the base dir restriction. More...
 
static mkdir ($path, $mode=0777)
 Create a directory (if not exists). More...
 
static rmdir ($path)
 Recursivly remove a directory, including all files. More...
 
static dirname ($path, $levels=1)
 Dirname that does not care about separator, and does not return '. More...
 
static basename ($path)
 Basename that does not care about separator, and also removes possible query parameters. More...
 
static ext ($filename, $lower_case=true)
 Get extension of a filename. More...
 
static changeExt ($filename, $ext)
 Change the extension of a filename. More...
 
static tempDir ()
 Temporarily directory of the system. More...
 
static tempFile ($ext='tmp', $data=null)
 Create a temporarily file. More...
 
static mtime ($filename)
 Optimistic filemtime (no need to check file_exists() first). More...
 
static unlink ($filename)
 Safe unlink. More...
 
static write ($filename, $data, $mode=null, $append=false)
 Write data to a file. More...
 
static read ($filename, $default=false)
 Read the content of a file. More...
 
static serialize ($filename, $data, $mode=null)
 Serialize data and write it to a file. More...
 
static unserialize ($filename, $default=false)
 Unserialize the content of a file. More...
 
static jsonEncode ($filename, $data, $mode=null, $options=JSON_PRETTY_PRINT)
 Encode data to JSON and write it to a file. More...
 
static jsonDecode ($filename, $default=false, $assoc=true)
 Decode the JSON content of a file. More...
 
static mime ($filename)
 Get MIME type for a file. More...
 
static find ($path, $filters=null, $recursive=false)
 Find and filter files. More...
 
static dir ($path, $pattern=null, $recursive=false)
 Find files. More...
 

Data Fields

const FIND_FILTER_TYPE = 'type'
 Specify the type (see FIND_TYPE_* constants). More...
 
const FIND_FILTER_NAME = 'name'
 Specify the name. More...
 
const FIND_FILTER_TIME = 'time'
 Specify the time (Unix format). More...
 
const FIND_FILTER_SIZE = 'size'
 Speficy the size (shorthand notation allowed). More...
 
const FIND_FILTER_FUNC = 'func'
 Specify a callback function (1st param = full name, 2nd param = info gathered). The. More...
 
const FIND_TYPE_DIR = 1
 
const FIND_TYPE_FILE = 2
 
const FIND_TYPE_ALL = 255
 
const EXT_MASK = '/\\.([^\\.\\/\\\\]*)$/'
 

Detailed Description

File and file system helper functions.

Definition at line 8 of file File.php.

Member Function Documentation

◆ addDirSeparator()

static addDirSeparator (   $path)
static

Add a directory separator to the path (if not present)

Parameters
string$path
Returns
string Path including the final directory separator.s

Definition at line 28 of file File.php.

◆ basename()

static basename (   $path)
static

Basename that does not care about separator, and also removes possible query parameters.

Parameters
string$path
Returns
string Base name of the given path.

Definition at line 102 of file File.php.

◆ changeExt()

static changeExt (   $filename,
  $ext 
)
static

Change the extension of a filename.

Parameters
string$filenameFilename.
string$extNew extension (with the leading dot, and optional characters before it).
Returns
string

Definition at line 120 of file File.php.

◆ dir()

static dir (   $path,
  $pattern = null,
  $recursive = false 
)
static

Find files.

Parameters
string$pathBase path.
string$patternFilename pattern (with '*' and '?' as possible wildcards).
bool$recursiveTrue to do a recursive search.
Returns
array Full names. False if path is not a directory.

Definition at line 299 of file File.php.

◆ dirname()

static dirname (   $path,
  $levels = 1 
)
static

Dirname that does not care about separator, and does not return '.

' when no directory.

Parameters
string$path
int$levelsNumber of levels to go up.
Returns
string Name of the directory of the given path.

Definition at line 93 of file File.php.

◆ ext()

static ext (   $filename,
  $lower_case = true 
)
static

Get extension of a filename.

Parameters
string$filenameFilename.
bool$lower_caseTrue to convert the extension to lower case.
Returns
string Extension (without the leading dot).

Definition at line 111 of file File.php.

◆ find()

static find (   $path,
  $filters = null,
  $recursive = false 
)
static

Find and filter files.

Parameters
string$pathBase path.
array$filtersArray with filters. Key = filter (see FIND_FILTER_* constants) plus optional operator (see Str::operator()), value = value to compare to.
bool$recursiveTrue to do a recursive search.
Returns
array Key = full name, value = info gathered for the filtering (key = filter key, value = entry value). False if path is not a directory.

Definition at line 254 of file File.php.

◆ inBaseDir()

static inBaseDir (   $filename)
static

Check if a filename is within the base dir restriction.

Parameters
string$filename
Returns
bool True if within the base dir, or no base dir at all.

Definition at line 58 of file File.php.

◆ inDir()

static inDir (   $path,
  $filename 
)
static

Check if a filename is within a directory.

Parameters
string$pathDirectory (absolute path).
string$filenameFilename (absolute path).
Returns
bool True if the file is within the directory.

Definition at line 45 of file File.php.

◆ jsonDecode()

static jsonDecode (   $filename,
  $default = false,
  $assoc = true 
)
static

Decode the JSON content of a file.

Parameters
string$filenameFile to read from.
mixed$defaultValue to return if the file does not exist.
bool$assocWhen true, returned objects will be converted into assoc.arrays.
Returns
mixed Decoded data.

Definition at line 232 of file File.php.

◆ jsonEncode()

static jsonEncode (   $filename,
  $data,
  $mode = null,
  $options = JSON_PRETTY_PRINT 
)
static

Encode data to JSON and write it to a file.

Parameters
string$filenameFile to write to.
mixed$dataData to decode and save.
int$modeFile mode (not set if empty).
int$optionsPHP's json_encode options.
Returns
int Number of bytes written or false on error.

Definition at line 222 of file File.php.

◆ mime()

static mime (   $filename)
static

Get MIME type for a file.

Parameters
string$filenameFilename.
Returns
string MIME type.

Definition at line 240 of file File.php.

◆ mkdir()

static mkdir (   $path,
  $mode = 0777 
)
static

Create a directory (if not exists).

Parameters
string$path,.
int$modeMode (read/write/exec) for the new directory.
Returns
bool True on success.

Definition at line 69 of file File.php.

◆ mtime()

static mtime (   $filename)
static

Optimistic filemtime (no need to check file_exists() first).

Parameters
string$filenameFile to get modification time for.
Returns
int Last modification time. False if file does not exists.

Definition at line 152 of file File.php.

◆ read()

static read (   $filename,
  $default = false 
)
static

Read the content of a file.

Parameters
string$filenameFile to read from.
mixed$defaultValue to return if the file does not exist.
Returns
mixed Data.

Definition at line 192 of file File.php.

◆ rmdir()

static rmdir (   $path)
static

Recursivly remove a directory, including all files.

Parameters
string$pathDirectory to remove.
Returns
bool True on success.

Definition at line 80 of file File.php.

◆ serialize()

static serialize (   $filename,
  $data,
  $mode = null 
)
static

Serialize data and write it to a file.

Parameters
string$filenameFile to write to.
mixed$dataData to serialize and save.
int$modeFile mode (not set if empty).
Returns
int Number of bytes written or false on error.

Definition at line 202 of file File.php.

◆ tempDir()

static tempDir ( )
static

Temporarily directory of the system.

Returns
string

Definition at line 127 of file File.php.

◆ tempFile()

static tempFile (   $ext = 'tmp',
  $data = null 
)
static

Create a temporarily file.

Parameters
string$extExtension of the temporarily file (without the leading dot).
string$dataData to put in the file (file will be created anyway if empty).
Returns
string Name of the temporarily file.

Definition at line 141 of file File.php.

◆ unlink()

static unlink (   $filename)
static

Safe unlink.

Parameters
string$filenameFile to delete.
Returns
bool True if file does not exist afterwards.

Definition at line 165 of file File.php.

◆ unserialize()

static unserialize (   $filename,
  $default = false 
)
static

Unserialize the content of a file.

Parameters
string$filenameFile to read from.
mixed$defaultValue to return if the file does not exist.
Returns
mixed Unserialized data.

Definition at line 211 of file File.php.

◆ windows()

static windows (   $filename)
static

Normalize a Windows filename.

Parameters
string$filename
Returns
string Filename with backward slashes as directory separator and all lower case (because: case insensitive).

Definition at line 36 of file File.php.

◆ write()

static write (   $filename,
  $data,
  $mode = null,
  $append = false 
)
static

Write data to a file.

Parameters
string$filenameFile to write to.
mixed$dataData to save.
int$modeFile mode (not set if empty).
bool$appendAppend on true, overwrite on false.
Returns
int Number of bytes written or false on error.

Definition at line 181 of file File.php.

Field Documentation

◆ EXT_MASK

const EXT_MASK = '/\\.([^\\.\\/\\\\]*)$/'

Definition at line 21 of file File.php.

◆ FIND_FILTER_FUNC

const FIND_FILTER_FUNC = 'func'

Specify a callback function (1st param = full name, 2nd param = info gathered). The.

Definition at line 14 of file File.php.

◆ FIND_FILTER_NAME

const FIND_FILTER_NAME = 'name'

Specify the name.

Definition at line 11 of file File.php.

◆ FIND_FILTER_SIZE

const FIND_FILTER_SIZE = 'size'

Speficy the size (shorthand notation allowed).

Definition at line 13 of file File.php.

◆ FIND_FILTER_TIME

const FIND_FILTER_TIME = 'time'

Specify the time (Unix format).

Definition at line 12 of file File.php.

◆ FIND_FILTER_TYPE

const FIND_FILTER_TYPE = 'type'

Specify the type (see FIND_TYPE_* constants).

Definition at line 10 of file File.php.

◆ FIND_TYPE_ALL

const FIND_TYPE_ALL = 255

Definition at line 19 of file File.php.

◆ FIND_TYPE_DIR

const FIND_TYPE_DIR = 1

Definition at line 17 of file File.php.

◆ FIND_TYPE_FILE

const FIND_TYPE_FILE = 2

Definition at line 18 of file File.php.


The documentation for this class was generated from the following file: