Convenience class for reading, writing and appending to files.
Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
Licensed under The MIT License
For full copyright and license information, please see the LICENSE.txt
Redistributions of files must retain the above copyright notice.
Methods summary
public
|
#
__construct( string $path, boolean $create = false, integer $mode = 0755 )
Constructor
Parameters
- $path
- Path to file
- $create
- Create file if it does not exist (if true)
- $mode
- Mode to apply to the folder holding the file
Link
|
public
|
#
__destruct( )
Closes the current file if it is opened
Closes the current file if it is opened
|
public
boolean
|
#
create( )
Creates the file.
Returns
boolean Success
|
public
boolean
|
#
open( string $mode = 'r', boolean $force = false )
Opens the current file with a given $mode
Opens the current file with a given $mode
Parameters
- $mode
- A valid 'fopen' mode string (r|w|a ...)
- $force
- If true then the file will be re-opened even if its already opened, otherwise it won't
Returns
boolean True on success, false on failure
|
public
string|false
|
#
read( string|boolean $bytes = false, string $mode = 'rb', boolean $force = false )
Return the contents of this file as a string.
Return the contents of this file as a string.
Parameters
- $bytes
- where to start
- $mode
- A
fread compatible mode.
- $force
- If true then the file will be re-opened even if its already opened, otherwise it won't
Returns
string|false string on success, false on failure
|
public
integer|boolean
|
#
offset( integer|boolean $offset = false, integer $seek = SEEK_SET )
Sets or gets the offset for the currently opened file.
Sets or gets the offset for the currently opened file.
Parameters
- $offset
- The $offset in bytes to seek. If set to false then the current offset is returned.
- $seek
- PHP Constant SEEK_SET | SEEK_CUR | SEEK_END determining what the $offset is relative to
Returns
integer|boolean True on success, false on failure (set mode), false on failure or integer offset on success (get mode)
|
public static
string
|
#
prepare( string $data, boolean $forceWindows = false )
Prepares an ASCII string for writing. Converts line endings to the
correct terminator for the current platform. If Windows, "\r\n" will be used,
all other platforms will use "\n"
Prepares an ASCII string for writing. Converts line endings to the
correct terminator for the current platform. If Windows, "\r\n" will be used,
all other platforms will use "\n"
Parameters
- $data
- Data to prepare for writing.
- $forceWindows
- If true forces Windows new line string.
Returns
string The with converted line endings.
|
public
boolean
|
#
write( string $data, string $mode = 'w', boolean $force = false )
Write given data to this file.
Write given data to this file.
Parameters
- $data
- Data to write to this File.
- $mode
- Mode of writing. See fwrite().
- $force
- Force the file to open
Returns
boolean Success
|
public
boolean
|
#
append( string $data, boolean $force = false )
Append given data string to this file.
Append given data string to this file.
Parameters
- $data
- Data to write
- $force
- Force the file to open
Returns
boolean Success
|
public
boolean
|
#
close( )
Closes the current file if it is opened.
Closes the current file if it is opened.
Returns
boolean True if closing was successful or file was already closed, otherwise false
|
public
boolean
|
#
delete( )
Deletes the file.
Returns
boolean Success
|
public
array
|
#
info( )
Returns the file info as an array with the following keys:
Returns the file info as an array with the following keys:
- dirname
- basename
- extension
- filename
- filesize
- mime
Returns
array File information.
|
public
string|false
|
#
ext( )
Returns the file extension.
Returns the file extension.
Returns
string|false The file extension, false if extension cannot be extracted.
|
public
string|false
|
#
name( )
Returns the file name without extension.
Returns the file name without extension.
Returns
string|false The file name without extension, false if name cannot be extracted.
|
public
string
|
#
safe( string|null $name = null, string|null $ext = null )
Makes file name safe for saving
Makes file name safe for saving
Parameters
- $name
- The name of the file to make safe if different from $this->name
- $ext
- The name of the extension to make safe if different from $this->ext
Returns
string The extension of the file
|
public
string|false
|
#
md5( integer|boolean $maxsize = 5 )
Get md5 Checksum of file with previous check of Filesize
Get md5 Checksum of file with previous check of Filesize
Parameters
- $maxsize
- in MB or true to force
Returns
|
public
string
|
#
pwd( )
Returns the full path of the file.
Returns the full path of the file.
Returns
string Full path to the file
|
public
boolean
|
#
exists( )
Returns true if the file exists.
Returns true if the file exists.
Returns
boolean True if it exists, false otherwise
|
public
string|false
|
#
perms( )
Returns the "chmod" (permissions) of the file.
Returns the "chmod" (permissions) of the file.
Returns
string|false Permissions for the file, or false in case of an error
|
public
integer|false
|
#
size( )
Returns the file size
Returns
integer|false Size of the file in bytes, or false in case of an error
|
public
boolean
|
#
writable( )
Returns true if the file is writable.
Returns true if the file is writable.
Returns
boolean True if it's writable, false otherwise
|
public
boolean
|
#
executable( )
Returns true if the File is executable.
Returns true if the File is executable.
Returns
boolean True if it's executable, false otherwise
|
public
boolean
|
#
readable( )
Returns true if the file is readable.
Returns true if the file is readable.
Returns
boolean True if file is readable, false otherwise
|
public
integer|false
|
#
owner( )
Returns the file's owner.
Returns the file's owner.
Returns
integer|false The file owner, or false in case of an error
|
public
integer|false
|
#
group( )
Returns the file's group.
Returns the file's group.
Returns
integer|false The file group, or false in case of an error
|
public
integer|false
|
#
lastAccess( )
Returns last access time.
Returns last access time.
Returns
integer|false Timestamp of last access time, or false in case of an error
|
public
integer|false
|
#
lastChange( )
Returns last modified time.
Returns last modified time.
Returns
integer|false Timestamp of last modification, or false in case of an error
|
public
integer|false
|
#
lines( )
Returns the numbers of lines in a text.
Returns the numbers of lines in a text.
Returns
integer|false The numbers of line in file, or false in case of an error
|
public
Liberty\Folder
|
#
folder( )
Returns the current folder.
Returns the current folder.
Returns
|
public
boolean
|
#
copy( string $dest, boolean $overwrite = true )
Copy the File to $dest
Parameters
- $dest
- Destination for the copy
- $overwrite
- Overwrite $dest if exists
Returns
boolean Success
|
public
false|string
|
#
mime( )
Gets the mime type of the file. Uses the finfo extension if
it's available, otherwise falls back to mime_content_type().
Gets the mime type of the file. Uses the finfo extension if
it's available, otherwise falls back to mime_content_type().
Returns
false|string The mimetype of the file, or false if reading fails.
|
public
|
#
clearStatCache( boolean $all = false )
Clear PHP's internal stat cache
Clear PHP's internal stat cache
Parameters
- $all
Clear all cache or not. Passing false will clear
the stat cache for the current path only.
|
public
boolean
|
#
replaceText( string|array $search, string|array $replace )
Searches for a given text and replaces the text if found.
Searches for a given text and replaces the text if found.
Parameters
- $search
- Text(s) to search for.
- $replace
- Text(s) to replace with.
Returns
boolean Success
|