Class File
Convenience class for reading, writing and appending to files.
Properties summary
- $Folder public
\Cake\Filesystem\Folder
Folder object of the file
- $handle public
resource|null
Holds the file handler resource if the file is opened
- $info public
array
File info
- $lock public
bool|null
Enable locking for file reading and writing
- $name public
string
File name
- $path public
string|null
Path property
Method Summary
- _basename() protected static
Returns the file basename. simulate the php basename() for multibyte (mb_basename).
- mime() public
Gets the mime type of the file. Uses the finfo extension if it's available, otherwise falls back to mime_content_type().
- prepare() public static
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"
Method Detail
__construct() public
__construct(mixed $path, mixed $create, mixed $mode)
Constructor
Parameters
-
string
$path Path to file
-
bool
$create optional Create file if it does not exist (if true)
-
int
$mode optional Mode to apply to the folder holding the file
Links
__destruct() public
__destruct()
Closes the current file if it is opened
_basename() protected static
_basename(mixed $path, mixed $ext)
Returns the file basename. simulate the php basename() for multibyte (mb_basename).
Parameters
-
string
$path Path to file
-
string|null
$ext optional The name of the extension
Returns
string
the file basename.
append() public
append(mixed $data, mixed $force)
Append given data string to this file.
Parameters
-
string
$data Data to write
-
bool
$force optional Force the file to open
Returns
bool
Success
clearStatCache() public
clearStatCache(mixed $all)
Clear PHP's internal stat cache
Parameters
-
bool
$all optional Clear all cache or not. Passing false will clear the stat cache for the current path only.
close() public
close()
Closes the current file if it is opened.
Returns
bool
True if closing was successful or file was already closed, otherwise false
copy() public
copy(mixed $dest, mixed $overwrite)
Copy the File to $dest
Parameters
-
string
$dest Absolute path to copy the file to.
-
bool
$overwrite optional Overwrite $dest if exists
Returns
bool
Success
create() public
create()
Creates the file.
Returns
bool
Success
delete() public
delete()
Deletes the file.
Returns
bool
Success
executable() public
executable()
Returns true if the File is executable.
Returns
bool
True if it's executable, false otherwise
exists() public
exists()
Returns true if the file exists.
Returns
bool
True if it exists, false otherwise
ext() public
ext()
Returns the file extension.
Returns
string|false
The file extension, false if extension cannot be extracted.
folder() public
folder()
Returns the current folder.
Returns
\Cake\Filesystem\Folder
Current folder
group() public
group()
Returns the file's group.
Returns
int|false
The file group, or false in case of an error
info() public
info()
Returns the file info as an array with the following keys:
- dirname
- basename
- extension
- filename
- filesize
- mime
Returns
array
File information.
lastAccess() public
lastAccess()
Returns last access time.
Returns
int|false
Timestamp of last access time, or false in case of an error
lastChange() public
lastChange()
Returns last modified time.
Returns
int|false
Timestamp of last modification, or false in case of an error
md5() public
md5(mixed $maxsize)
Get md5 Checksum of file with previous check of Filesize
Parameters
-
int|bool
$maxsize optional in MB or true to force
Returns
string|false
md5 Checksum {@link https://secure.php.net/md5_file See md5_file()}, or false in case of an error
mime() public
mime()
Gets the mime type of the file. Uses the finfo extension if it's available, otherwise falls back to mime_content_type().
Returns
string|false
The mimetype of the file, or false if reading fails.
name() public
name()
Returns the file name without extension.
Returns
string|false
The file name without extension, false if name cannot be extracted.
offset() public
offset(mixed $offset, mixed $seek)
Sets or gets the offset for the currently opened file.
Parameters
-
int|bool
$offset optional The $offset in bytes to seek. If set to false then the current offset is returned.
-
int
$seek optional PHP Constant SEEK_SET | SEEK_CUR | SEEK_END determining what the $offset is relative to
Returns
int|bool
True on success, false on failure (set mode), false on failure or integer offset on success (get mode)
open() public
open(mixed $mode, mixed $force)
Opens the current file with a given $mode
Parameters
-
string
$mode optional A valid 'fopen' mode string (r|w|a ...)
-
bool
$force optional If true then the file will be re-opened even if its already opened, otherwise it won't
Returns
bool
True on success, false on failure
owner() public
owner()
Returns the file's owner.
Returns
int|false
The file owner, or false in case of an error
perms() public
perms()
Returns the "chmod" (permissions) of the file.
Returns
string|false
Permissions for the file, or false in case of an error
prepare() public static
prepare(mixed $data, mixed $forceWindows)
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
-
string
$data Data to prepare for writing.
-
bool
$forceWindows optional If true forces Windows new line string.
Returns
string
The with converted line endings.
pwd() public
pwd()
Returns the full path of the file.
Returns
string
Full path to the file
read() public
read(mixed $lengthInBytes, mixed $mode, mixed $force)
Return the contents of this file as a string.
Parameters
-
int|false
$lengthInBytes optional The length to read in bytes or
false
for the full file. Defaults tofalse
.-
string
$mode optional A
fread
compatible mode.-
bool
$force optional 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
readable() public
readable()
Returns true if the file is readable.
Returns
bool
True if file is readable, false otherwise
replaceText() public
replaceText(mixed $search, mixed $replace)
Searches for a given text and replaces the text if found.
Parameters
-
string|array
$search Text(s) to search for.
-
string|array
$replace Text(s) to replace with.
Returns
bool
Success
safe() public
safe(mixed $name, mixed $ext)
Makes file name safe for saving
Parameters
-
string|null
$name optional The name of the file to make safe if different from $this->name
-
string|null
$ext optional The name of the extension to make safe if different from $this->ext
Returns
string
The extension of the file
size() public
size()
Returns the file size
Returns
int|false
Size of the file in bytes, or false in case of an error
writable() public
writable()
Returns true if the file is writable.
Returns
bool
True if it's writable, false otherwise
write() public
write(mixed $data, mixed $mode, mixed $force)
Write given data to this file.
Parameters
-
string
$data Data to write to this File.
-
string
$mode optional Mode of writing. {@link https://secure.php.net/fwrite See fwrite()}.
-
bool
$force optional Force the file to open
Returns
bool
Success
Property Detail
$Folder public
Folder object of the file
Type
\Cake\Filesystem\Folder
$handle public
Holds the file handler resource if the file is opened
Type
resource|null
$info public
File info
Type
array
$lock public
Enable locking for file reading and writing
Type
bool|null
$name public
File name
Type
string
$path public
Path property
Current file's absolute path
Type
string|null
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.9/class-Cake.Filesystem.File.html