PHP Class F

Inheritance: extends E
Mostra file Open project: getkirby/toolkit Class Usage Examples

Public Properties

Property Type Description
$mimes
$types
$units

Public Methods

Method Description
append ( string $file, mixed $content ) : boolean Appends new content to an existing file
base64 ( string $file ) : string Returns the file content as base64 encoded string
copy ( string $file, string $target ) : boolean Copy a file to a new location.
dirname ( string $file ) : string Just an alternative for dirname() to stay consistent
download ( $file, $name = null ) * Automatically sends all needed headers for the file to be downloaded and echos the file's content
exists ( string $file ) : boolean Checks if a file exists
extension ( string $file, string $extension = false ) : string Gets the extension of a file
extensionToMime ( string $extension ) : string Converts a file extension to a mime type
extensionToType ( string $extension ) : string Returns the file type for a passed extension
extensions ( string $type = null ) : array Returns all extensions for a certain file type
filename ( string $name ) : string Extracts the filename from a file path
is ( string $file, string $value ) : boolean Checks if a file is of a certain type
isReadable ( string $file ) : boolean Checks if the file is readable
isWritable ( string $file ) : boolean Checks if the file is writable
load ( $file, $data = [] ) Safely requires a file if it exists
mime ( string $file ) : mixed Returns the mime type of a file
mimeToExtension ( string $mime ) : string Converts a mime type to a file extension
mimeToType ( string $mime ) : string Returns the type for a given mime
mimes ( ) : array Returns all detectable mime types
modified ( string $file, string $format = null, string $handler = 'date' ) : integer Get the file's last modification time.
move ( string $old, string $new ) : boolean Moves a file to a new location
name ( string $name ) : string Extracts the name from a file path or filename without extension
niceSize ( mixed $size ) : string Converts an integer size into a human readable format
read ( string $file ) : mixed Reads the content of a file
remove ( string $file ) : boolean Deletes a file
resolve ( string $base, array $extensions ) : string | false Tries to find a file by various extensions
safeName ( string $string ) : string Sanitize a filename to strip unwanted special characters
show ( string $file ) Read and send the file with the correct headers
size ( mixed $file ) : mixed Returns the size of a file.
type ( string $file ) : string Categorize the file
types ( ) : array Returns an array of all available file types
unzip ( string $file, string $to ) : boolean Unzips a zip file
uri ( string $file ) : string Returns the file as data uri
write ( string $file, mixed $content, boolean $append = false ) : boolean Creates a new file

Method Details

append() public static method

Appends new content to an existing file
public static append ( string $file, mixed $content ) : boolean
$file string The path for the file
$content mixed Either a string or an array. Arrays will be converted to JSON.
return boolean

base64() public static method

Returns the file content as base64 encoded string
public static base64 ( string $file ) : string
$file string The path for the file
return string

copy() public static method

Copy a file to a new location.
public static copy ( string $file, string $target ) : boolean
$file string
$target string
return boolean

dirname() public static method

$dirname = f::dirname('/var/www/test.txt'); dirname is /var/www
public static dirname ( string $file ) : string
$file string The path
return string

download() public static method

* Automatically sends all needed headers for the file to be downloaded and echos the file's content
public static download ( $file, $name = null )
$file The root to the file
$name Optional filename for the download

exists() public static method

Checks if a file exists
public static exists ( string $file ) : boolean
$file string
return boolean

extension() public static method

$extension = f::extension('test.txt'); extension is txt
public static extension ( string $file, string $extension = false ) : string
$file string The filename or path
$extension string Set an optional extension to overwrite the current one
return string

extensionToMime() public static method

Converts a file extension to a mime type
public static extensionToMime ( string $extension ) : string
$extension string
return string

extensionToType() public static method

Returns the file type for a passed extension
public static extensionToType ( string $extension ) : string
$extension string
return string

extensions() public static method

Returns all extensions for a certain file type
public static extensions ( string $type = null ) : array
$type string
return array

filename() public static method

$filename = f::filename('/var/www/test.txt'); filename is test.txt
public static filename ( string $name ) : string
$name string The path
return string

is() public static method

Checks if a file is of a certain type
public static is ( string $file, string $value ) : boolean
$file string Full path to the file
$value string An extension or mime type
return boolean

isReadable() public static method

Checks if the file is readable
public static isReadable ( string $file ) : boolean
$file string
return boolean

isWritable() public static method

Checks if the file is writable
public static isWritable ( string $file ) : boolean
$file string
return boolean

load() public static method

Safely requires a file if it exists
public static load ( $file, $data = [] )

mime() public static method

Returns the mime type of a file
public static mime ( string $file ) : mixed
$file string
return mixed

mimeToExtension() public static method

Converts a mime type to a file extension
public static mimeToExtension ( string $mime ) : string
$mime string
return string

mimeToType() public static method

Returns the type for a given mime
public static mimeToType ( string $mime ) : string
$mime string
return string

mimes() public static method

Returns all detectable mime types
public static mimes ( ) : array
return array

modified() public static method

Get the file's last modification time.
public static modified ( string $file, string $format = null, string $handler = 'date' ) : integer
$file string
$format string
$handler string date or strftime
return integer

move() public static method

$move = f::move('test.txt', 'super.txt'); if($move) echo 'The file has been moved';
public static move ( string $old, string $new ) : boolean
$old string The current path for the file
$new string The path to the new location
return boolean

name() public static method

$name = f::name('/var/www/test.txt'); name is test
public static name ( string $name ) : string
$name string The path or filename
return string

niceSize() public static method

$niceSize = f::niceSize('/path/to/a/file.txt'); nice size is i.e: 212 kb $niceSize = f::niceSize(1231939); nice size is: 1,2 mb
public static niceSize ( mixed $size ) : string
$size mixed The file size or a file path
return string

read() public static method

$content = f::read('test.txt'); i.e. content is hello $content = f::read('text.txt', 'json'); returns an array with the parsed content
public static read ( string $file ) : mixed
$file string The path for the file
return mixed

remove() public static method

$remove = f::remove('test.txt'); if($remove) echo 'The file has been removed';
public static remove ( string $file ) : boolean
$file string The path for the file
return boolean

resolve() public static method

Tries to find a file by various extensions
public static resolve ( string $base, array $extensions ) : string | false
$base string
$extensions array
return string | false

safeName() public static method

$safe = f::safeName('über genious.txt'); safe will be ueber-genious.txt
public static safeName ( string $string ) : string
$string string The file name
return string

show() public static method

Read and send the file with the correct headers
public static show ( string $file )
$file string

size() public static method

$size = f::size('/var/www/test.txt'); size is ie: 1231939
public static size ( mixed $file ) : mixed
$file mixed The path
return mixed

type() public static method

Categorize the file
public static type ( string $file ) : string
$file string Either the file path or extension
return string

types() public static method

Returns an array of all available file types
public static types ( ) : array
return array

unzip() public static method

Unzips a zip file
public static unzip ( string $file, string $to ) : boolean
$file string
$to string
return boolean

uri() public static method

Returns the file as data uri
public static uri ( string $file ) : string
$file string The path for the file
return string

write() public static method

f::write('test.txt', 'hello'); creates a new text file with hello as content create a new file f::write('text.txt', array('test' => 'hello')); creates a new file and encodes the array as json
public static write ( string $file, mixed $content, boolean $append = false ) : boolean
$file string The path for the new file
$content mixed Either a string, an object or an array. Arrays and objects will be serialized.
$append boolean true: append the content to an exisiting file if available. false: overwrite.
return boolean

Property Details

$mimes public_oe static_oe property

public static $mimes

$types public_oe static_oe property

public static $types

$units public_oe static_oe property

public static $units