\Cake\FilesystemFile

Convenience class for reading, writing and appending to files.

Summary

Methods
Properties
Constants
__construct()
__destruct()
create()
open()
read()
offset()
prepare()
write()
append()
close()
delete()
info()
ext()
name()
safe()
md5()
pwd()
exists()
perms()
size()
writable()
executable()
readable()
owner()
group()
lastAccess()
lastChange()
folder()
copy()
mime()
clearStatCache()
replaceText()
$Folder
$name
$info
$handle
$lock
$path
No constants found
_basename()
No protected properties found
N/A
No private methods found
No private properties found
N/A

Properties

$name

$name : string

File name

Type

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

$info

$info : array

File info

Type

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

$handle

$handle : resource|null

Holds the file handler resource if the file is opened

Type

resource|null — https://book.cakephp.org/3.0/en/core-libraries/file-folder.html#Cake\Filesystem\File::$handle

$lock

$lock : boolean|null

Enable locking for file reading and writing

Type

boolean|null — https://book.cakephp.org/3.0/en/core-libraries/file-folder.html#Cake\Filesystem\File::$lock

$path

$path : string|null

Path property

Current file's absolute path

Type

string|null — https://book.cakephp.org/3.0/en/core-libraries/file-folder.html#Cake\Filesystem\File::$path

Methods

__construct()

__construct(string  $path, boolean  $create = false, integer  $mode = 493) 

Constructor

Parameters

string $path

Path to file

boolean $create

Create file if it does not exist (if true)

integer $mode

Mode to apply to the folder holding the file

__destruct()

__destruct() 

Closes the current file if it is opened

create()

create() : boolean

Creates the file.

Returns

boolean —

Success

open()

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

Opens the current file with a given $mode

Parameters

string $mode

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

boolean $force

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

Returns

boolean —

True on success, false on failure

read()

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

Return the contents of this file as a string.

Parameters

string|boolean $bytes

where to start

string $mode

A fread compatible mode.

boolean $force

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

offset()

offset(integer|boolean  $offset = false, integer  $seek = SEEK_SET) : integer|boolean

Sets or gets the offset for the currently opened file.

Parameters

integer|boolean $offset

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

integer $seek

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

Returns

integer|boolean —

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

prepare()

prepare(string  $data, boolean  $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.

boolean $forceWindows

If true forces Windows new line string.

Returns

string —

The with converted line endings.

write()

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

Write given data to this file.

Parameters

string $data

Data to write to this File.

string $mode

Mode of writing. {@link https://secure.php.net/fwrite See fwrite()}.

boolean $force

Force the file to open

Returns

boolean —

Success

append()

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

Append given data string to this file.

Parameters

string $data

Data to write

boolean $force

Force the file to open

Returns

boolean —

Success

close()

close() : boolean

Closes the current file if it is opened.

Returns

boolean —

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

delete()

delete() : boolean

Deletes the file.

Returns

boolean —

Success

info()

info() : array

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

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

Returns

array —

File information.

ext()

ext() : string|false

Returns the file extension.

Returns

string|false —

The file extension, false if extension cannot be extracted.

name()

name() : string|false

Returns the file name without extension.

Returns

string|false —

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

safe()

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

Makes file name safe for saving

Parameters

string|null $name

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

string|null $ext

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

Returns

string —

The extension of the file

md5()

md5(integer|boolean  $maxsize = 5) : string|false

Get md5 Checksum of file with previous check of Filesize

Parameters

integer|boolean $maxsize

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

pwd()

pwd() : string

Returns the full path of the file.

Returns

string —

Full path to the file

exists()

exists() : boolean

Returns true if the file exists.

Returns

boolean —

True if it exists, false otherwise

perms()

perms() : string|false

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

Returns

string|false —

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

size()

size() : integer|false

Returns the file size

Returns

integer|false —

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

writable()

writable() : boolean

Returns true if the file is writable.

Returns

boolean —

True if it's writable, false otherwise

executable()

executable() : boolean

Returns true if the File is executable.

Returns

boolean —

True if it's executable, false otherwise

readable()

readable() : boolean

Returns true if the file is readable.

Returns

boolean —

True if file is readable, false otherwise

owner()

owner() : integer|false

Returns the file's owner.

Returns

integer|false —

The file owner, or false in case of an error

group()

group() : integer|false

Returns the file's group.

Returns

integer|false —

The file group, or false in case of an error

lastAccess()

lastAccess() : integer|false

Returns last access time.

Returns

integer|false —

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

lastChange()

lastChange() : integer|false

Returns last modified time.

Returns

integer|false —

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

folder()

folder() : \Cake\Filesystem\Folder

Returns the current folder.

Returns

\Cake\Filesystem\Folder

Current folder

copy()

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

Copy the File to $dest

Parameters

string $dest

Absolute path to copy the file to.

boolean $overwrite

Overwrite $dest if exists

Returns

boolean —

Success

mime()

mime() : false|string

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

Returns

false|string —

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

clearStatCache()

clearStatCache(boolean  $all = false) : void

Clear PHP's internal stat cache

Parameters

boolean $all

Clear all cache or not. Passing false will clear the stat cache for the current path only.

replaceText()

replaceText(string|array  $search, string|array  $replace) : boolean

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

boolean —

Success

_basename()

_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

The name of the extension

Returns

string —

the file basename.