W3cubDocs

/CakePHP 4.4

Class
File

Convenience class for reading, writing and appending to files.

Namespace: Cake\Filesystem
Deprecated: 4.0.0 Will be removed in 5.0.

Property 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<string, mixed>

    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

  • __construct() public

    Constructor

  • __destruct() public

    Closes the current file if it is opened

  • _basename() protected static

    Returns the file basename. simulate the php basename() for multibyte (mb_basename).

  • append() public

    Append given data string to this file.

  • clearStatCache() public

    Clear PHP's internal stat cache

  • close() public

    Closes the current file if it is opened.

  • copy() public

    Copy the File to $dest

  • create() public

    Creates the file.

  • delete() public

    Deletes the file.

  • executable() public

    Returns true if the File is executable.

  • exists() public

    Returns true if the file exists.

  • ext() public

    Returns the file extension.

  • folder() public

    Returns the current folder.

  • group() public

    Returns the file's group.

  • info() public

    Returns the file info as an array with the following keys:

  • lastAccess() public

    Returns last access time.

  • lastChange() public

    Returns last modified time.

  • md5() public

    Get md5 Checksum of file with previous check of Filesize

  • mime() public

    Gets the mime type of the file. Uses the finfo extension if it's available, otherwise falls back to mime_content_type().

  • name() public

    Returns the file name without extension.

  • offset() public

    Sets or gets the offset for the currently opened file.

  • open() public

    Opens the current file with a given $mode

  • owner() public

    Returns the file's owner.

  • perms() public

    Returns the "chmod" (permissions) of the file.

  • 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"

  • pwd() public

    Returns the full path of the file.

  • read() public

    Return the contents of this file as a string.

  • readable() public

    Returns true if the file is readable.

  • replaceText() public

    Searches for a given text and replaces the text if found.

  • safe() public

    Makes file name safe for saving

  • size() public

    Returns the file size

  • writable() public

    Returns true if the file is writable.

  • write() public

    Write given data to this file.

Method Detail

__construct() public

__construct(string $path, bool $create = false, int $mode = 0755)

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

https://book.cakephp.org/4/en/core-libraries/file-folder.html#file-api

__destruct() public

__destruct()

Closes the current file if it is opened

_basename() protected static

_basename(string $path, string|null $ext = null): string

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

append() public

append(string $data, bool $force = false): bool

Append given data string to this file.

Parameters

string $data

Data to write

bool $force optional

Force the file to open

Returns

bool

clearStatCache() public

clearStatCache(bool $all = false): void

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.

Returns

void

close() public

close(): bool

Closes the current file if it is opened.

Returns

bool

copy() public

copy(string $dest, bool $overwrite = true): bool

Copy the File to $dest

Parameters

string $dest

Absolute path to copy the file to.

bool $overwrite optional

Overwrite $dest if exists

Returns

bool

create() public

create(): bool

Creates the file.

Returns

bool

delete() public

delete(): bool

Deletes the file.

Returns

bool

executable() public

executable(): bool

Returns true if the File is executable.

Returns

bool

exists() public

exists(): bool

Returns true if the file exists.

Returns

bool

ext() public

ext(): string|false

Returns the file extension.

Returns

string|false

folder() public

folder(): Cake\Filesystem\Folder

Returns the current folder.

Returns

Cake\Filesystem\Folder

group() public

group(): int|false

Returns the file's group.

Returns

int|false

info() public

info(): array<string, mixed>

Returns the file info as an array with the following keys:

  • dirname
  • basename
  • extension
  • filename
  • filesize
  • mime

Returns

array<string, mixed>

lastAccess() public

lastAccess(): int|false

Returns last access time.

Returns

int|false

lastChange() public

lastChange(): int|false

Returns last modified time.

Returns

int|false

md5() public

md5(int|true $maxsize = 5): string|false

Get md5 Checksum of file with previous check of Filesize

Parameters

int|true $maxsize optional

in MB or true to force

Returns

string|false

mime() public

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().

Returns

string|false

name() public

name(): string|false

Returns the file name without extension.

Returns

string|false

offset() public

offset(int|false $offset = false, int $seek = SEEK_SET): int|bool

Sets or gets the offset for the currently opened file.

Parameters

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

Returns

int|bool

open() public

open(string $mode = 'r', bool $force = false): bool

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

owner() public

owner(): int|false

Returns the file's owner.

Returns

int|false

perms() public

perms(): string|false

Returns the "chmod" (permissions) of the file.

Returns

string|false

prepare() public static

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"

Parameters

string $data

Data to prepare for writing.

bool $forceWindows optional

If true forces Windows new line string.

Returns

string

pwd() public

pwd(): string|null

Returns the full path of the file.

Returns

string|null

read() public

read(string|false $bytes = false, string $mode = 'rb', bool $force = false): string|false

Return the contents of this file as a string.

Parameters

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

Returns

string|false

readable() public

readable(): bool

Returns true if the file is readable.

Returns

bool

replaceText() public

replaceText(array<string>|string $search, array<string>|string $replace): bool

Searches for a given text and replaces the text if found.

Parameters

array<string>|string $search

Text(s) to search for.

array<string>|string $replace

Text(s) to replace with.

Returns

bool

safe() public

safe(string|null $name = null, string|null $ext = null): string

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

size() public

size(): int|false

Returns the file size

Returns

int|false

writable() public

writable(): bool

Returns true if the file is writable.

Returns

bool

write() public

write(string $data, string $mode = 'w', bool $force = false): bool

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

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<string, mixed>

$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/4.4/class-Cake.Filesystem.File.html