Class yii\helpers\BaseFileHelper

Inheritanceyii\helpers\BaseFileHelper
Subclassesyii\helpers\FileHelper
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseFileHelper.php

BaseFileHelper provides concrete implementation for yii\helpers\FileHelper.

Do not use BaseFileHelper. Use yii\helpers\FileHelper instead.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$mimeAliasesFile string The path (or alias) of a PHP file containing MIME aliases. yii\helpers\BaseFileHelper
$mimeMagicFile string The path (or alias) of a PHP file containing MIME type information. yii\helpers\BaseFileHelper

Public Methods

Hide inherited methods

MethodDescriptionDefined By
copyDirectory() Copies a whole directory as another one. yii\helpers\BaseFileHelper
createDirectory() Creates a new directory. yii\helpers\BaseFileHelper
filterPath() Checks if the given file path satisfies the filtering options. yii\helpers\BaseFileHelper
findDirectories() Returns the directories found under the specified directory and subdirectories. yii\helpers\BaseFileHelper
findFiles() Returns the files found under the specified directory and subdirectories. yii\helpers\BaseFileHelper
getExtensionsByMimeType() Determines the extensions by given MIME type. yii\helpers\BaseFileHelper
getMimeType() Determines the MIME type of the specified file. yii\helpers\BaseFileHelper
getMimeTypeByExtension() Determines the MIME type based on the extension name of the specified file. yii\helpers\BaseFileHelper
localize() Returns the localized version of a specified file. yii\helpers\BaseFileHelper
normalizePath() Normalizes a file/directory path. yii\helpers\BaseFileHelper
removeDirectory() Removes a directory (and all its content) recursively. yii\helpers\BaseFileHelper
unlink() Removes a file or symlink in a cross-platform way yii\helpers\BaseFileHelper

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
loadMimeAliases() Loads MIME aliases from the specified file. yii\helpers\BaseFileHelper
loadMimeTypes() Loads MIME types from the specified file. yii\helpers\BaseFileHelper
normalizeOptions() yii\helpers\BaseFileHelper

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
PATTERN_CASE_INSENSITIVE 32 yii\helpers\BaseFileHelper
PATTERN_ENDSWITH 4 yii\helpers\BaseFileHelper
PATTERN_MUSTBEDIR 8 yii\helpers\BaseFileHelper
PATTERN_NEGATIVE 16 yii\helpers\BaseFileHelper
PATTERN_NODIR 1 yii\helpers\BaseFileHelper

Property Details

$mimeAliasesFile public static property (available since version 2.0.14)

The path (or alias) of a PHP file containing MIME aliases.

public static string $mimeAliasesFile '@yii/helpers/mimeAliases.php'
$mimeMagicFile public static property

The path (or alias) of a PHP file containing MIME type information.

public static string $mimeMagicFile '@yii/helpers/mimeTypes.php'

Method Details

copyDirectory() public static method

Copies a whole directory as another one.

The files and sub-directories will also be copied over.

public static void copyDirectory ( $src, $dst, $options = [] )
$src string

The source directory

$dst string

The destination directory

$options array

Options for directory copy. Valid options are:

  • dirMode: integer, the permission to be set for newly copied directories. Defaults to 0775.
  • fileMode: integer, the permission to be set for newly copied files. Defaults to the current environment setting.
  • filter: callback, a PHP callback that is called for each directory or file. The signature of the callback should be: function ($path), where $path refers the full path to be filtered. The callback can return one of the following values:

    • true: the directory or file will be copied (the "only" and "except" options will be ignored)
    • false: the directory or file will NOT be copied (the "only" and "except" options will be ignored)
    • null: the "only" and "except" options will determine whether the directory or file should be copied
  • only: array, list of patterns that the file paths should match if they want to be copied. A path matches a pattern if it contains the pattern string at its end. For example, '.php' matches all file paths ending with '.php'. Note, the '/' characters in a pattern matches both '/' and '\' in the paths. If a file path matches a pattern in both "only" and "except", it will NOT be copied.
  • except: array, list of patterns that the files or directories should match if they want to be excluded from being copied. A path matches a pattern if it contains the pattern string at its end. Patterns ending with '/' apply to directory paths only, and patterns not ending with '/' apply to file paths only. For example, '/a/b' matches all file paths ending with '/a/b'; and '.svn/' matches directory paths ending with '.svn'. Note, the '/' characters in a pattern matches both '/' and '\' in the paths.
  • caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to true.
  • recursive: boolean, whether the files under the subdirectories should also be copied. Defaults to true.
  • beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. If the callback returns false, the copy operation for the sub-directory or file will be cancelled. The signature of the callback should be: function ($from, $to), where $from is the sub-directory or file to be copied from, while $to is the copy target.
  • afterCopy: callback, a PHP callback that is called after each sub-directory or file is successfully copied. The signature of the callback should be: function ($from, $to), where $from is the sub-directory or file copied from, while $to is the copy target.
  • copyEmptyDirectories: boolean, whether to copy empty directories. Set this to false to avoid creating directories that do not contain files. This affects directories that do not contain files initially as well as directories that do not contain files at the target destination because files have been filtered via only or except. Defaults to true. This option is available since version 2.0.12. Before 2.0.12 empty directories are always copied.
throws yii\base\InvalidArgumentException

if unable to open directory

createDirectory() public static method

Creates a new directory.

This method is similar to the PHP mkdir() function except that it uses chmod() to set the permission of the created directory in order to avoid the impact of the umask setting.

public static boolean createDirectory ( $path, $mode 0775, $recursive true )
$path string

Path of the directory to be created.

$mode integer

The permission to be set for the created directory.

$recursive boolean

Whether to create parent directories if they do not exist.

return boolean

Whether the directory is created successfully

throws yii\base\Exception

if the directory could not be created (i.e. php error due to parallel changes)

filterPath() public static method

Checks if the given file path satisfies the filtering options.

public static boolean filterPath ( $path, $options )
$path string

The path of the file or directory to be checked

$options array

The filtering options. See findFiles() for explanations of the supported options.

return boolean

Whether the file or directory satisfies the filtering options.

findDirectories() public static method (available since version 2.0.14)

Returns the directories found under the specified directory and subdirectories.

public static array findDirectories ( $dir, $options = [] )
$dir string

The directory under which the files will be looked for.

$options array

Options for directory searching. Valid options are:

  • filter: callback, a PHP callback that is called for each directory or file. The signature of the callback should be: function ($path), where $path refers the full path to be filtered. The callback can return one of the following values:

    • true: the directory will be returned
    • false: the directory will NOT be returned
  • recursive: boolean, whether the files under the subdirectories should also be looked for. Defaults to true.

return array

Directories found under the directory, in no particular order. Ordering depends on the files system used.

throws yii\base\InvalidArgumentException

if the dir is invalid.

findFiles() public static method

Returns the files found under the specified directory and subdirectories.

public static array findFiles ( $dir, $options = [] )
$dir string

The directory under which the files will be looked for.

$options array

Options for file searching. Valid options are:

  • filter: callback, a PHP callback that is called for each directory or file. The signature of the callback should be: function ($path), where $path refers the full path to be filtered. The callback can return one of the following values:

    • true: the directory or file will be returned (the only and except options will be ignored)
    • false: the directory or file will NOT be returned (the only and except options will be ignored)
    • null: the only and except options will determine whether the directory or file should be returned
  • except: array, list of patterns excluding from the results matching file or directory paths. Patterns ending with slash ('/') apply to directory paths only, and patterns not ending with '/' apply to file paths only. For example, '/a/b' matches all file paths ending with '/a/b'; and .svn/ matches directory paths ending with .svn. If the pattern does not contain a slash (/), it is treated as a shell glob pattern and checked for a match against the pathname relative to $dir. Otherwise, the pattern is treated as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match a / in the pathname. For example, views/*.php matches views/index.php but not views/controller/index.php. A leading slash matches the beginning of the pathname. For example, /*.php matches index.php but not views/start/index.php. An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again. If a negated pattern matches, this will override lower precedence patterns sources. Put a backslash (\) in front of the first ! for patterns that begin with a literal !, for example, \!important!.txt. Note, the '/' characters in a pattern matches both '/' and '\' in the paths.
  • only: array, list of patterns that the file paths should match if they are to be returned. Directory paths are not checked against them. Same pattern matching rules as in the except option are used. If a file path matches a pattern in both only and except, it will NOT be returned.
  • caseSensitive: boolean, whether patterns specified at only or except should be case sensitive. Defaults to true.
  • recursive: boolean, whether the files under the subdirectories should also be looked for. Defaults to true.
return array

Files found under the directory, in no particular order. Ordering depends on the files system used.

throws yii\base\InvalidArgumentException

if the dir is invalid.

getExtensionsByMimeType() public static method

Determines the extensions by given MIME type.

This method will use a local map between extension names and MIME types.

public static array getExtensionsByMimeType ( $mimeType, $magicFile null )
$mimeType string

File MIME type.

$magicFile string

The path (or alias) of the file that contains all available MIME type information. If this is not set, the file specified by $mimeMagicFile will be used.

return array

The extensions corresponding to the specified MIME type

getMimeType() public static method

Determines the MIME type of the specified file.

This method will first try to determine the MIME type based on finfo_open. If the fileinfo extension is not installed, it will fall back to getMimeTypeByExtension() when $checkExtension is true.

public static string|null getMimeType ( $file, $magicFile null, $checkExtension true )
$file string

The file name.

$magicFile string

Name of the optional magic database file (or alias), usually something like /path/to/magic.mime. This will be passed as the second parameter to finfo_open() when the fileinfo extension is installed. If the MIME type is being determined based via getMimeTypeByExtension() and this is null, it will use the file specified by $mimeMagicFile.

$checkExtension boolean

Whether to use the file extension to determine the MIME type in case finfo_open() cannot determine it.

return string|null

The MIME type (e.g. text/plain). Null is returned if the MIME type cannot be determined.

throws yii\base\InvalidConfigException

when the fileinfo PHP extension is not installed and $checkExtension is false.

getMimeTypeByExtension() public static method

Determines the MIME type based on the extension name of the specified file.

This method will use a local map between extension names and MIME types.

public static string|null getMimeTypeByExtension ( $file, $magicFile null )
$file string

The file name.

$magicFile string

The path (or alias) of the file that contains all available MIME type information. If this is not set, the file specified by $mimeMagicFile will be used.

return string|null

The MIME type. Null is returned if the MIME type cannot be determined.

loadMimeAliases() protected static method (available since version 2.0.14)

Loads MIME aliases from the specified file.

protected static array loadMimeAliases ( $aliasesFile )
$aliasesFile string

The path (or alias) of the file that contains MIME type aliases. If this is not set, the file specified by $mimeAliasesFile will be used.

return array

The mapping from file extensions to MIME types

loadMimeTypes() protected static method

Loads MIME types from the specified file.

protected static array loadMimeTypes ( $magicFile )
$magicFile string

The path (or alias) of the file that contains all available MIME type information. If this is not set, the file specified by $mimeMagicFile will be used.

return array

The mapping from file extensions to MIME types

localize() public static method

Returns the localized version of a specified file.

The searching is based on the specified language code. In particular, a file with the same name will be looked for under the subdirectory whose name is the same as the language code. For example, given the file "path/to/view.php" and language code "zh-CN", the localized file will be looked for as "path/to/zh-CN/view.php". If the file is not found, it will try a fallback with just a language code that is "zh" i.e. "path/to/zh/view.php". If it is not found as well the original file will be returned.

If the target and the source language codes are the same, the original file will be returned.

public static string localize ( $file, $language null, $sourceLanguage null )
$file string

The original file

$language string

The target language that the file should be localized to. If not set, the value of yii\base\Application::$language will be used.

$sourceLanguage string

The language that the original file is in. If not set, the value of yii\base\Application::$sourceLanguage will be used.

return string

The matching localized file, or the original file if the localized version is not found. If the target and the source language codes are the same, the original file will be returned.

normalizeOptions() protected static method (available since version 2.0.12)

protected static array normalizeOptions ( array $options )
$options array

Raw options

return array

Normalized options

normalizePath() public static method

Normalizes a file/directory path.

The normalization does the following work:

  • Convert all directory separators into DIRECTORY_SEPARATOR (e.g. "\a/b\c" becomes "/a/b/c")
  • Remove trailing directory separators (e.g. "/a/b/c/" becomes "/a/b/c")
  • Turn multiple consecutive slashes into a single one (e.g. "/a///b/c" becomes "/a/b/c")
  • Remove ".." and "." based on their meanings (e.g. "/a/./b/../c" becomes "/a/c")

Note: For registered stream wrappers, the consecutive slashes rule and ".."/"." translations are skipped.

public static string normalizePath ( $path, $ds DIRECTORY_SEPARATOR )
$path string

The file/directory path to be normalized

$ds string

The directory separator to be used in the normalized result. Defaults to DIRECTORY_SEPARATOR.

return string

The normalized file/directory path

removeDirectory() public static method

Removes a directory (and all its content) recursively.

public static void removeDirectory ( $dir, $options = [] )
$dir string

The directory to be deleted recursively.

$options array

Options for directory remove. Valid options are:

  • traverseSymlinks: boolean, whether symlinks to the directories should be traversed too. Defaults to false, meaning the content of the symlinked directory would not be deleted. Only symlink would be removed in that default case.
throws yii\base\ErrorException

in case of failure

unlink() public static method (available since version 2.0.14)

Removes a file or symlink in a cross-platform way

public static boolean unlink ( $path )
$path string