Convenience class for reading, writing and appending to files.
\Cake\Filesystem\Folder
Folder object of the file
resource|null
Holds the file handler resource if the file is opened
array
File info
bool|null
Enable locking for file reading and writing
string
File name
string|null
Path property
Returns the file basename. simulate the php basename() for multibyte (mb_basename).
Gets the mime type of the file. Uses the finfo extension if it's available, otherwise falls back to mime_content_type().
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"
__construct(string $path, bool $create, int $mode)
Constructor
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
__destruct()
Closes the current file if it is opened
_basename(string $path, ?string $ext)
Returns the file basename. simulate the php basename() for multibyte (mb_basename).
string
$path Path to file
string|null
$ext optional The name of the extension
string
the file basename.
append(string $data, bool $force)
Append given data string to this file.
string
$data Data to write
bool
$force optional Force the file to open
bool
Success
clearStatCache(mixed $all)
Clear PHP's internal stat cache
bool
$all optional Clear all cache or not. Passing false will clear the stat cache for the current path only.
close()
Closes the current file if it is opened.
bool
True if closing was successful or file was already closed, otherwise false
copy(string $dest, bool $overwrite)
Copy the File to $dest
string
$dest Absolute path to copy the file to.
bool
$overwrite optional Overwrite $dest if exists
bool
Success
create()
Creates the file.
bool
Success
delete()
Deletes the file.
bool
Success
executable()
Returns true if the File is executable.
bool
True if it's executable, false otherwise
exists()
Returns true if the file exists.
bool
True if it exists, false otherwise
ext()
Returns the file extension.
string|false
The file extension, false if extension cannot be extracted.
folder()
Returns the current folder.
\Cake\Filesystem\Folder
Current folder
group()
Returns the file's group.
int|false
The file group, or false in case of an error
info()
Returns the file info as an array with the following keys:
array
File information.
lastAccess()
Returns last access time.
int|false
Timestamp of last access time, or false in case of an error
lastChange()
Returns last modified time.
int|false
Timestamp of last modification, or false in case of an error
md5(mixed $maxsize)
Get md5 Checksum of file with previous check of Filesize
int|true
$maxsize optional in MB or true to force
string|false
md5 Checksum {@link https://secure.php.net/md5_file See md5_file()}, or false in case of an error.
mime()
Gets the mime type of the file. Uses the finfo extension if it's available, otherwise falls back to mime_content_type().
string|false
The mimetype of the file, or false if reading fails.
name()
Returns the file name without extension.
string|false
The file name without extension, false if name cannot be extracted.
offset(mixed $offset, int $seek)
Sets or gets the offset for the currently opened file.
int|false
$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
int|bool
True on success, false on failure (set mode), false on failure or integer offset on success (get mode).
open(string $mode, bool $force)
Opens the current file with a given $mode
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
bool
True on success, false on failure
owner()
Returns the file's owner.
int|false
The file owner, or bool in case of an error
perms()
Returns the "chmod" (permissions) of the file.
string|false
Permissions for the file, or false in case of an error
prepare(string $data, bool $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"
string
$data Data to prepare for writing.
bool
$forceWindows optional If true forces Windows new line string.
string
The with converted line endings.
pwd()
Returns the full path of the file.
string|false
Full path to the file, or false on failure
read(mixed $bytes, string $mode, bool $force)
Return the contents of this file as a string.
string|false
$bytes optional where to start
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
string|false
String on success, false on failure
readable()
Returns true if the file is readable.
bool
True if file is readable, false otherwise
replaceText(mixed $search, mixed $replace)
Searches for a given text and replaces the text if found.
string|array
$search Text(s) to search for.
string|array
$replace Text(s) to replace with.
bool
Success
safe(?string $name, ?string $ext)
Makes file name safe for saving
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
string
The extension of the file
size()
Returns the file size
int|false
Size of the file in bytes, or false in case of an error
writable()
Returns true if the file is writable.
bool
True if it's writable, false otherwise
write(string $data, string $mode, bool $force)
Write given data to this file.
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
bool
Success
Folder object of the file
\Cake\Filesystem\Folder
Holds the file handler resource if the file is opened
resource|null
File info
array
Enable locking for file reading and writing
bool|null
File name
string
Path property
Current file's absolute path
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/4.1/class-Cake.Filesystem.File.html