Fr3nch13/CakePHP Utilities

File
in package

Convenience class for reading, writing and appending to files.

Will be removed in 5.0.

Table of Contents

Properties

$Folder  : Folder
Folder object of the file
$handle  : resource|null
Holds the file handler resource if the file is opened
$info  : array<string, mixed>
File info
$lock  : bool|null
Enable locking for file reading and writing
$name  : string
File name
$path  : string|null
Path property

Methods

__construct()  : mixed
Constructor
__destruct()  : mixed
Closes the current file if it is opened
append()  : bool
Append given data string to this file.
clearStatCache()  : void
Clear PHP's internal stat cache
close()  : bool
Closes the current file if it is opened.
copy()  : bool
Copy the File to $dest
create()  : bool
Creates the file.
delete()  : bool
Deletes the file.
executable()  : bool
Returns true if the File is executable.
exists()  : bool
Returns true if the file exists.
ext()  : string|false
Returns the file extension.
folder()  : Folder
Returns the current folder.
group()  : int|false
Returns the file's group.
info()  : array<string, mixed>
Returns the file info as an array with the following keys:
lastAccess()  : int|false
Returns last access time.
lastChange()  : int|false
Returns last modified time.
md5()  : string|false
Get md5 Checksum of file with previous check of Filesize
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().
name()  : string|false
Returns the file name without extension.
offset()  : int|bool
Sets or gets the offset for the currently opened file.
open()  : bool
Opens the current file with a given $mode
owner()  : int|false
Returns the file's owner.
perms()  : string|false
Returns the "chmod" (permissions) of the file.
prepare()  : 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"
pwd()  : string|null
Returns the full path of the file.
read()  : string|false
Return the contents of this file as a string.
readable()  : bool
Returns true if the file is readable.
replaceText()  : bool
Searches for a given text and replaces the text if found.
safe()  : string
Makes file name safe for saving
size()  : int|false
Returns the file size
writable()  : bool
Returns true if the file is writable.
write()  : bool
Write given data to this file.
_basename()  : string
Returns the file basename. simulate the php basename() for multibyte (mb_basename).

Properties

$handle

Holds the file handler resource if the file is opened

public resource|null $handle

https://book.cakephp.org/4/en/core-libraries/file-folder.html#Cake\Filesystem\File::$handle

$info

File info

public array<string, mixed> $info = []

https://book.cakephp.org/4/en/core-libraries/file-folder.html#Cake\Filesystem\File::$info

$lock

Enable locking for file reading and writing

public bool|null $lock

https://book.cakephp.org/4/en/core-libraries/file-folder.html#Cake\Filesystem\File::$lock

$name

File name

public string $name

https://book.cakephp.org/4/en/core-libraries/file-folder.html#Cake\Filesystem\File::$name

$path

Path property

public string|null $path

Current file's absolute path

https://book.cakephp.org/4/en/core-libraries/file-folder.html#Cake\Filesystem\File::$path

Methods

__destruct()

Closes the current file if it is opened

public __destruct() : mixed

append()

Append given data string to this file.

public append(string $data[, bool $force = false ]) : bool
Parameters
$data : string

Data to write

$force : bool = false

Force the file to open

Return values
bool

Success

clearStatCache()

Clear PHP's internal stat cache

public clearStatCache([bool $all = false ]) : void
Parameters
$all : bool = false

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.

public close() : bool
Return values
bool

True if closing was successful or file was already closed, otherwise false

copy()

Copy the File to $dest

public copy(string $dest[, bool $overwrite = true ]) : bool
Parameters
$dest : string

Absolute path to copy the file to.

$overwrite : bool = true

Overwrite $dest if exists

Return values
bool

Success

create()

Creates the file.

public create() : bool
Return values
bool

Success

delete()

Deletes the file.

public delete() : bool
Return values
bool

Success

executable()

Returns true if the File is executable.

public executable() : bool
Return values
bool

True if it's executable, false otherwise

exists()

Returns true if the file exists.

public exists() : bool
Return values
bool

True if it exists, false otherwise

ext()

Returns the file extension.

public ext() : string|false
Return values
string|false

The file extension, false if extension cannot be extracted.

folder()

Returns the current folder.

public folder() : Folder
Return values
Folder

Current folder

group()

Returns the file's group.

public group() : int|false
Return values
int|false

The file group, or false in case of an error

info()

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

public info() : array<string, mixed>
  • dirname
  • basename
  • extension
  • filename
  • filesize
  • mime
Return values
array<string, mixed>

File information.

lastAccess()

Returns last access time.

public lastAccess() : int|false
Return values
int|false

Timestamp of last access time, or false in case of an error

lastChange()

Returns last modified time.

public lastChange() : int|false
Return values
int|false

Timestamp of last modification, or false in case of an error

md5()

Get md5 Checksum of file with previous check of Filesize

public md5([int|true $maxsize = 5 ]) : string|false
Parameters
$maxsize : int|true = 5

in MB or true to force

Return values
string|false

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

public mime() : string|false
Return values
string|false

The mimetype of the file, or false if reading fails.

name()

Returns the file name without extension.

public name() : string|false
Return values
string|false

The file name without extension, false if name cannot be extracted.

offset()

Sets or gets the offset for the currently opened file.

public offset([int|false $offset = false ][, int $seek = SEEK_SET ]) : int|bool
Parameters
$offset : int|false = false

The $offset in bytes to seek. If set to false then the current offset is returned.

$seek : int = SEEK_SET

PHP Constant SEEK_SET | SEEK_CUR | SEEK_END determining what the $offset is relative to

Return values
int|bool

True on success, false on failure (set mode), false on failure or integer offset on success (get mode).

open()

Opens the current file with a given $mode

public open([string $mode = 'r' ][, bool $force = false ]) : bool
Parameters
$mode : string = 'r'

A valid 'fopen' mode string (r|w|a ...)

$force : bool = false

If true then the file will be re-opened even if its already opened, otherwise it won't

Return values
bool

True on success, false on failure

owner()

Returns the file's owner.

public owner() : int|false
Return values
int|false

The file owner, or bool in case of an error

perms()

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

public perms() : string|false
Return values
string|false

Permissions for the file, or false in case of an error

prepare()

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"

public static prepare(string $data[, bool $forceWindows = false ]) : string
Parameters
$data : string

Data to prepare for writing.

$forceWindows : bool = false

If true forces Windows new line string.

Return values
string

The with converted line endings.

pwd()

Returns the full path of the file.

public pwd() : string|null
Return values
string|null

Full path to the file, or null on failure

read()

Return the contents of this file as a string.

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

where to start

$mode : string = 'rb'

A fread compatible mode.

$force : bool = false

If true then the file will be re-opened even if its already opened, otherwise it won't

Return values
string|false

String on success, false on failure

readable()

Returns true if the file is readable.

public readable() : bool
Return values
bool

True if file is readable, false otherwise

replaceText()

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

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

Text(s) to search for.

$replace : array<string|int, string>|string

Text(s) to replace with.

Return values
bool

Success

safe()

Makes file name safe for saving

public safe([string|null $name = null ][, string|null $ext = null ]) : string
Parameters
$name : string|null = null

The name of the file to make safe if different from $this->name

$ext : string|null = null

The name of the extension to make safe if different from $this->ext

Return values
string

The extension of the file

size()

Returns the file size

public size() : int|false
Return values
int|false

Size of the file in bytes, or false in case of an error

writable()

Returns true if the file is writable.

public writable() : bool
Return values
bool

True if it's writable, false otherwise

write()

Write given data to this file.

public write(string $data[, string $mode = 'w' ][, bool $force = false ]) : bool
Parameters
$data : string

Data to write to this File.

$mode : string = 'w'

Mode of writing. See fwrite().

$force : bool = false

Force the file to open

Return values
bool

Success

_basename()

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

protected static _basename(string $path[, string|null $ext = null ]) : string
Parameters
$path : string

Path to file

$ext : string|null = null

The name of the extension

Return values
string

the file basename.


        
On this page

Search results