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<string, mixed>
File info
bool|null
Enable locking for file reading and writing
string
File name
string|null
Path property
Constructor
Closes the current file if it is opened
Returns the file basename. simulate the php basename() for multibyte (mb_basename).
Append given data string to this file.
Clear PHP's internal stat cache
Closes the current file if it is opened.
Copy the File to $dest
Creates the file.
Deletes the file.
Returns true if the File is executable.
Returns true if the file exists.
Returns the file extension.
Returns the current folder.
Returns the file's group.
Returns the file info as an array with the following keys:
Returns last access time.
Returns last modified time.
Get md5 Checksum of file with previous check of Filesize
Gets the mime type of the file. Uses the finfo extension if it's available, otherwise falls back to mime_content_type().
Returns the file name without extension.
Sets or gets the offset for the currently opened file.
Opens the current file with a given $mode
Returns the file's owner.
Returns the "chmod" (permissions) of the file.
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"
Returns the full path of the file.
Return the contents of this file as a string.
Returns true if the file is readable.
Searches for a given text and replaces the text if found.
Makes file name safe for saving
Returns the file size
Returns true if the file is writable.
Write given data to this file.
__construct(string $path, bool $create = false, int $mode = 0755)
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|null $ext = null): string
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
append(string $data, bool $force = false): bool
Append given data string to this file.
string
$data Data to write
bool
$force optional Force the file to open
bool
clearStatCache(bool $all = false): void
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.
void
close(): bool
Closes the current file if it is opened.
bool
copy(string $dest, bool $overwrite = true): bool
Copy the File to $dest
string
$dest Absolute path to copy the file to.
bool
$overwrite optional Overwrite $dest if exists
bool
create(): bool
Creates the file.
bool
delete(): bool
Deletes the file.
bool
executable(): bool
Returns true if the File is executable.
bool
exists(): bool
Returns true if the file exists.
bool
ext(): string|false
Returns the file extension.
string|false
folder(): Cake\Filesystem\Folder
Returns the current folder.
Cake\Filesystem\Folder
group(): int|false
Returns the file's group.
int|false
info(): array<string, mixed>
Returns the file info as an array with the following keys:
array<string, mixed>
lastAccess(): int|false
Returns last access time.
int|false
lastChange(): int|false
Returns last modified time.
int|false
md5(int|true $maxsize = 5): string|false
Get md5 Checksum of file with previous check of Filesize
int|true
$maxsize optional in MB or true to force
string|false
mime(): string|false
Gets the mime type of the file. Uses the finfo extension if it's available, otherwise falls back to mime_content_type().
string|false
name(): string|false
Returns the file name without extension.
string|false
offset(int|false $offset = false, int $seek = SEEK_SET): int|bool
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
open(string $mode = 'r', bool $force = false): bool
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
owner(): int|false
Returns the file's owner.
int|false
perms(): string|false
Returns the "chmod" (permissions) of the file.
string|false
prepare(string $data, bool $forceWindows = false): string
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
pwd(): string|null
Returns the full path of the file.
string|null
read(string|false $bytes = false, string $mode = 'rb', bool $force = false): string|false
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
readable(): bool
Returns true if the file is readable.
bool
replaceText(array<string>|string $search, array<string>|string $replace): bool
Searches for a given text and replaces the text if found.
array<string>|string
$search Text(s) to search for.
array<string>|string
$replace Text(s) to replace with.
bool
safe(string|null $name = null, string|null $ext = null): string
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
size(): int|false
Returns the file size
int|false
writable(): bool
Returns true if the file is writable.
bool
write(string $data, string $mode = 'w', bool $force = false): bool
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
Folder object of the file
Cake\Filesystem\Folder
Holds the file handler resource if the file is opened
resource|null
File info
array<string, mixed>
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.4/class-Cake.Filesystem.File.html