Documentation

FileSystem
in package

File System Collection of file system manipulation tools

Tags
copyright

CLOUDREXX CMS - CLOUDREXX AG

author

Janik Tschanz janik.tschanz@comvation.com

author

Reto Kohli reto.kohli@comvation.com

subpackage

lib_filesystem

Table of Contents

Constants

CHMOD_FILE  = 0664
chmod mode for files (rw-rw-r--)
CHMOD_FOLDER  = 0777
chmod mode for folders (rwxrwxrwx)
CHMOD_GROUP_EXECUTE  = 8
CHMOD_GROUP_READ  = 32
CHMOD_GROUP_WRITE  = 16
CHMOD_OTHER_EXECUTE  = 1
CHMOD_OTHER_READ  = 4
CHMOD_OTHER_WRITE  = 2
CHMOD_USER_EXECUTE  = 64
CHMOD_USER_READ  = 256
CHMOD_USER_WRITE  = 128
DOT_LENGTH  = 1
ERROR_CANNOT_CREATE_FILE  = 101
ERROR_CANNOT_CREATE_FOLDER  = 102
ERROR_CANNOT_MOVE_FILE  = 111
ERROR_FILE_NOT_FOUND  = 1
ERROR_FILESIZE_TOO_BIG  = 202
ERROR_FOLDER_NOT_FOUND  = 2
ERROR_INVALID_FILETYPE  = 201
ERROR_MISSING_ARGUMENT  = 301
ERROR_NONE  = 0
Internal error numbers, stored in $error
MAX_FILENAME_LENGTH  = 255
Define some constants which are relevant for the filename

Properties

$error  : int
Internal error number

Methods

checkWebPath()  : mixed
chmod()  : bool
Applies the flags to the given path
clean_path()  : void
Makes a valid file path
copy_folder()  : bool
Copies a folder recursively from the source to the target path
copyDir()  : mixed
Copy a non-privileged folder to a non-privileged location.
copyFile()  : mixed
Copy a non-privileged file to a non-privileged location.
delete_file()  : bool
Deletes the file from the path specified
delete_folder()  : bool
Deletes the given folder name from the path
delFile()  : mixed
exists()  : bool
Wrapper for file_exists()
getError()  : int
Returns the current error number, if any, or zero.
getErrorString()  : int
Returns the current error string, if any, or the empty string.
hasPhpWriteAccess()  : mixed
Check if PHP has write access to this installations files
isValidFilePath()  : bool
Verify if $path is a valid file path on which file manipulation is allowed.
make_folder()  : bool
Creates the folder for the given path
makeWritable()  : bool
Add write access to $path.
move()  : bool
Renames or moves a file or folder
path_absolute_to_os_root()  : mixed
path_relative_to_root()  : void
Takes the path given by reference and removes any leading folders up to and including the ASCMS_PATH_OFFSET, including path separators (\ and /).
replaceCharacters()  : mixed
sanitizeFile()  : bool|string
Sanitizes the given file name.
sanitizePath()  : bool|string
Sanitizes the given path.
setChmod()  : mixed
touch()  : mixed
upload_file_http()  : bool
Moves an uploaded file to a specified path
callAddEvent()  : void
Call Indexer event to add ToDo: Use \Cx\Core\MediaSource\Model\Entity\LocalFile when FileSystem work smart
callDeleteEvent()  : void
Call Indexer event to delete ToDo: Use \Cx\Core\MediaSource\Model\Entity\LocalFile when FileSystem work smart
callDeleteEventStatic()  : mixed
Same as callDeleteEvent, but static
callEvent()  : mixed
callEventStatic()  : mixed
Same as callEvent, but static
callUpdateEvent()  : void
Call Indexer event to update ToDo: Use \Cx\Core\MediaSource\Model\Entity\LocalFile when FileSystem work smart

Constants

CHMOD_FILE

chmod mode for files (rw-rw-r--)

public mixed CHMOD_FILE = 0664

Note that 0664 is sufficient in most cases. (if it's not, it's not safe).

CHMOD_FOLDER

chmod mode for folders (rwxrwxrwx)

public mixed CHMOD_FOLDER = 0777

Note that 0775 is not sufficient for moving uploaded files (in many cases anyway).

ERROR_CANNOT_CREATE_FOLDER

public mixed ERROR_CANNOT_CREATE_FOLDER = 102

ERROR_NONE

Internal error numbers, stored in $error

public mixed ERROR_NONE = 0

MAX_FILENAME_LENGTH

Define some constants which are relevant for the filename

public mixed MAX_FILENAME_LENGTH = 255

Properties

$error

Internal error number

private static int $error = self::ERROR_NONE

See getError().

Methods

checkWebPath()

public checkWebPath(mixed $webPath) : mixed
Parameters
$webPath : mixed

chmod()

Applies the flags to the given path

public static chmod(string $path, int $flags) : bool

The path may be a file or a folder. The flags are considered to be octal values, as required by chmod().

Parameters
$path : string

The path to be chmodded

$flags : int

The flags to apply

Return values
bool

True on success, false otherwise

clean_path()

Makes a valid file path

public static clean_path(string &$path) : void

Replaces non-ASCII and some other characters in the string given by reference with underscores.

Parameters
$path : string

The path (or any other string)

Tags
todo

Test!

todo

Replace non-ASCII charactes with octal values

copy_folder()

Copies a folder recursively from the source to the target path

public static copy_folder(string $source_path, string $target_path[, string $force = false ][, mixed $privileged = false ]) : bool

If $force is true, the folder and its contents are copied even if a folder of the same name exists in the target path already. Otherwise, false is returned.

Parameters
$source_path : string

The path of the source folder

$target_path : string

The path of the target folder

$force : string = false

Force copying if true

$privileged : mixed = false
Return values
bool

True on success, false otherwise

copyDir()

Copy a non-privileged folder to a non-privileged location.

public copyDir(mixed $orgPath, mixed $orgWebPath, mixed $orgDirName, mixed $newPath, mixed $newWebPath, mixed $newDirName[, mixed $ignoreExists = false ]) : mixed

Use static::copy_folder() instead.

Parameters
$orgPath : mixed
$orgWebPath : mixed
$orgDirName : mixed
$newPath : mixed
$newWebPath : mixed
$newDirName : mixed
$ignoreExists : mixed = false
Tags
todo

Drop this method is favor of static::copy_folder(). Migrate logic of $ignoreExists to the usages of this method when migrating to static::copy_folder().

todo

Migrate logic of 'prevent infinit copy loop' to static::copy_folder().

copyFile()

Copy a non-privileged file to a non-privileged location.

public copyFile(mixed $orgPath, mixed $orgFileName, mixed $newPath, mixed $newFileName[, mixed $ignoreExists = false ]) : mixed
Parameters
$orgPath : mixed
$orgFileName : mixed
$newPath : mixed
$newFileName : mixed
$ignoreExists : mixed = false
Tags
todo

Drop this method is favor of File::copy(). Migrate logic of $ignoreExists to the usages of this method when migrating to File::copy().

delete_file()

Deletes the file from the path specified

public static delete_file(string $file_path) : bool

Returns true if the file doesn't exist in the first place.

Parameters
$file_path : string

The path of the file

Return values
bool

True on success, false otherwise

delete_folder()

Deletes the given folder name from the path

public static delete_folder(string $folder_path[, bool $force = false ][, mixed $privileged = false ]) : bool

If $force is true, recursively deletes any content of the folder first. Otherwise, if the folder is not empty, false is returned. Returns true if the folder was deleted.

Parameters
$folder_path : string

The folder path

$force : bool = false

If true, deletes contents of the folder

$privileged : mixed = false
Return values
bool

True on success, false otherwise

delFile()

public delFile(mixed $path, mixed $webPath, mixed $fileName) : mixed
Parameters
$path : mixed
$webPath : mixed
$fileName : mixed

exists()

Wrapper for file_exists()

public static exists(string $path) : bool

Prepends \Env::get('cx')->getWebsiteDocumentRootPath() to the path. The file is stat()ed before calling file_exists() in order to update a potentially outdated cache.

Parameters
$path : string

The file or folder path

Return values
bool

True if the file exists, false otherwise

getError()

Returns the current error number, if any, or zero.

public static getError() : int

Note that the internal $error variable is cleared, so you SHOULD call this once and get a sensible result.

Return values
int

The error number, or zero

getErrorString()

Returns the current error string, if any, or the empty string.

public static getErrorString() : int

Calls getError(), thus clearing the error number in the $error class variable.

Return values
int

The error number, or zero

hasPhpWriteAccess()

Check if PHP has write access to this installations files

public static hasPhpWriteAccess() : mixed

isValidFilePath()

Verify if $path is a valid file path on which file manipulation is allowed.

public static isValidFilePath(mixed $path[, bool $privileged = false ][, bool $sanitizePath = true ]) : bool

A valid file path is one of the following locations:

  • /images
  • /media
  • /themes
  • /tmp/session-
Parameters
$path : mixed
$privileged : bool = false

Set to TRUE to assess every path within the document root as TRUE and not only the ones listed above.

$sanitizePath : bool = true

Set to false to not sanitize the path. If set to true (default), then $path will be converted into its absolute realpath on the filesystem before its being validated.

Return values
bool

True if $path is a file located in one of the allowed locations.

make_folder()

Creates the folder for the given path

public static make_folder(string $folder_path[, bool $recursive = false ][, bool $privileged = false ]) : bool

If the path already exists, returns true if it's a folder, or false if it's a file.

Parameters
$folder_path : string

The directory path.

$recursive : bool = false

Allows the creation of nested directories specified in the directory.

$privileged : bool = false

Allows the creation of directories in privileged locations.

Return values
bool

True on success, false otherwise

makeWritable()

Add write access to $path.

public static makeWritable(string $path[, bool $privileged = false ]) : bool
Parameters
$path : string

Path to file or folder

$privileged : bool = false

Set to true if $path is a privileged file system location (see FileSystemFile::$privileged).

Return values
bool

true if write access on $path is set.

move()

Renames or moves a file or folder

public static move(string $from_path, string $to_path[, bool $force = false ][, bool $privileged = false ]) : bool

If a file or folder with the $to_path already exists, and $force is false, returns false.

Parameters
$from_path : string

The original path

$to_path : string

The destination path

$force : bool = false

Overwrites the destination if true

$privileged : bool = false

Set to true to allow operation on privileged file system locations (see FileSystemFile::$privileged).

Return values
bool

True on success, false otherwise

path_absolute_to_os_root()

public static path_absolute_to_os_root(mixed &$path) : mixed
Parameters
$path : mixed

path_relative_to_root()

Takes the path given by reference and removes any leading folders up to and including the ASCMS_PATH_OFFSET, including path separators (\ and /).

public static path_relative_to_root(string &$path) : void

Important note: The regex used to cut away the excess path is non-greedy and works fine in most cases. However, there is a small risk that it may go wrong if two things occur at the same time, namely:

  • the ASCMS_PATH_OFFSET is not part of the path provided, and
  • the path contains a folder or file with the same name. If this is the case, you can either change the offset or the name of the subfolder or file, whichever is acceptable.
Parameters
$path : string

Any absolute or relative path

Tags
author

Reto Kohli reto.kohli@comvation.com

since
2.2.0

replaceCharacters()

public static replaceCharacters(mixed $string) : mixed
Parameters
$string : mixed

sanitizeFile()

Sanitizes the given file name.

public static sanitizeFile(string $file) : bool|string
Parameters
$file : string
Return values
bool|string

$file

sanitizePath()

Sanitizes the given path.

public static sanitizePath(string $path) : bool|string
Parameters
$path : string
Return values
bool|string

$path

setChmod()

public setChmod(mixed $path, mixed $webPath, mixed $fileName) : mixed
Parameters
$path : mixed
$webPath : mixed
$fileName : mixed

touch()

public static touch(mixed $path) : mixed
Parameters
$path : mixed

upload_file_http()

Moves an uploaded file to a specified path

public static upload_file_http(string $upload_field_name, string &$target_path[, int $maximum_size = 0 ][, string $accepted_types = false ]) : bool

Returns true if the file name is valid, the file type matches one of the accepted file types, if specified, is not too large, and can be moved successfully to its target folder. Missing folders are created. If this fails, returns false. Mind that the target path MUST NOT include ASCMS_PATH, and SHOULD not include ASCMS_PATH_OFFSET. The latter will be cut off, however. The $target_path argument, given by reference, is fixed accordingly. If the file name found in $upload_field_name is empty, returns the empty string. Non-positive values for $maximum_size are ignored, as are empty values for $accepted_types.

Parameters
$upload_field_name : string

File input field name

$target_path : string

Target path, relative to the document root, including the file name, by reference.

$maximum_size : int = 0

The optional maximum allowed file size

$accepted_types : string = false

The optional allowed MIME type

Tags
author

Reto Kohli reto.kohli@comvation.com

since
2.2.0
Return values
bool

True on success, the empty string if there is nothing to do, or false otherwise

callAddEvent()

Call Indexer event to add ToDo: Use \Cx\Core\MediaSource\Model\Entity\LocalFile when FileSystem work smart

protected callAddEvent(mixed $path, mixed $name) : void
Parameters
$path : mixed

string path to file or directory

$name : mixed

string name of file or directory

Tags
throws
EventManagerException

callDeleteEvent()

Call Indexer event to delete ToDo: Use \Cx\Core\MediaSource\Model\Entity\LocalFile when FileSystem work smart

protected callDeleteEvent(mixed $path, mixed $name) : void
Parameters
$path : mixed

string path to file or directory

$name : mixed

string name of file or directory

Tags
throws
EventManagerException

callDeleteEventStatic()

Same as callDeleteEvent, but static

protected static callDeleteEventStatic(mixed $path, mixed $name) : mixed
Parameters
$path : mixed
$name : mixed
Tags
see
callDeleteEvent()

callEvent()

protected callEvent(mixed $event, mixed $params) : mixed
Parameters
$event : mixed
$params : mixed

callEventStatic()

Same as callEvent, but static

protected static callEventStatic(mixed $event, mixed $params) : mixed

Use non-static method instead

Parameters
$event : mixed
$params : mixed
Tags
see
callEvent()

callUpdateEvent()

Call Indexer event to update ToDo: Use \Cx\Core\MediaSource\Model\Entity\LocalFile when FileSystem work smart

protected callUpdateEvent(mixed $path, mixed $name, mixed $oldname) : void
Parameters
$path : mixed

string path to file or directory

$name : mixed

string name of file or directory

$oldname : mixed

string old name of file or directory

Tags
throws
EventManagerException

        
On this page

Search results