0 關注者

類別 yii\helpers\FileHelper

繼承關係yii\helpers\FileHelper » yii\helpers\BaseFileHelper
自版本起可用2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/helpers/FileHelper.php

檔案系統輔助工具。

公開屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$mimeAliasesFile string 包含 MIME 別名的 PHP 檔案路徑(或別名)。 yii\helpers\BaseFileHelper
$mimeExtensionsFile string 包含每個 MIME 類型的擴展名的 PHP 檔案路徑(或別名)。 yii\helpers\BaseFileHelper
$mimeMagicFile string 包含 MIME 類型資訊的 PHP 檔案路徑(或別名)。 yii\helpers\BaseFileHelper

公開方法

隱藏繼承的方法

方法 描述 定義於
changeOwnership() 變更檔案或目錄的 Unix 使用者和/或群組所有權,以及可選的模式。 yii\helpers\BaseFileHelper
copyDirectory() 將整個目錄複製為另一個目錄。 yii\helpers\BaseFileHelper
createDirectory() 建立新的目錄。 yii\helpers\BaseFileHelper
filterPath() 檢查給定的檔案路徑是否滿足篩選選項。 yii\helpers\BaseFileHelper
findDirectories() 傳回在指定目錄和子目錄下找到的目錄。 yii\helpers\BaseFileHelper
findFiles() 傳回在指定目錄和子目錄下找到的檔案。 yii\helpers\BaseFileHelper
getExtensionByMimeType() 根據給定的 MIME 類型判斷最常見的擴展名。 yii\helpers\BaseFileHelper
getExtensionsByMimeType() 根據給定的 MIME 類型判斷擴展名。 yii\helpers\BaseFileHelper
getMimeType() 判斷指定檔案的 MIME 類型。 yii\helpers\BaseFileHelper
getMimeTypeByExtension() 根據指定檔案的擴展名判斷 MIME 類型。 yii\helpers\BaseFileHelper
localize() 傳回指定檔案的本地化版本。 yii\helpers\BaseFileHelper
normalizePath() 標準化檔案/目錄路徑。 yii\helpers\BaseFileHelper
removeDirectory() 遞迴地移除目錄(及其所有內容)。 yii\helpers\BaseFileHelper
unlink() 以跨平台的方式移除檔案或符號連結 yii\helpers\BaseFileHelper

受保護的方法

隱藏繼承的方法

方法 描述 定義於
loadMimeAliases() 從指定的檔案載入 MIME 別名。 yii\helpers\BaseFileHelper
loadMimeExtensions() 從指定的檔案載入 MIME 擴展名。 yii\helpers\BaseFileHelper
loadMimeTypes() 從指定的檔案載入 MIME 類型。 yii\helpers\BaseFileHelper
normalizeOptions() yii\helpers\BaseFileHelper

常數

隱藏繼承的常數

常數 描述 定義於
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

方法詳情

隱藏繼承的方法

changeOwnership() public static method (自版本 2.0.43 起可用)

定義於: yii\helpers\BaseFileHelper::changeOwnership()

變更檔案或目錄的 Unix 使用者和/或群組所有權,以及可選的模式。

注意:此函數在遠端檔案上將無法運作,因為要檢查的檔案必須可透過伺服器的檔案系統存取。注意:在 Windows 上,此函數應用於常規檔案時會靜默失敗。

public static void changeOwnership ( $path, $ownership, $mode null )
$path string

檔案或目錄的路徑。

$ownership string|array|integer|null

檔案或目錄的使用者和/或群組所有權。當 $ownership 是字串時,格式為 'user:group',其中兩者都是可選的。例如,'user' 或 'user:' 將僅變更使用者,':group' 將僅變更群組,'user:group' 將變更兩者。當 $owners 是索引陣列時,格式為 [0 => user, 1 => group],例如 [$myUser, $myGroup]。也可以傳遞關聯陣列,例如 ['user' => $myUser, 'group' => $myGroup]。如果 $owners 是整數,它將用作使用者 ID。如果傳遞 null、空陣列或空字串,則不會變更所有權。

$mode integer|null

要為檔案或目錄設定的權限。如果傳遞 null,則不會變更模式。

                public static function changeOwnership($path, $ownership, $mode = null)
{
    if (!file_exists((string)$path)) {
        throw new InvalidArgumentException('Unable to change ownership, "' . $path . '" is not a file or directory.');
    }
    if (empty($ownership) && $ownership !== 0 && $mode === null) {
        return;
    }
    $user = $group = null;
    if (!empty($ownership) || $ownership === 0 || $ownership === '0') {
        if (is_int($ownership)) {
            $user = $ownership;
        } elseif (is_string($ownership)) {
            $ownerParts = explode(':', $ownership);
            $user = $ownerParts[0];
            if (count($ownerParts) > 1) {
                $group = $ownerParts[1];
            }
        } elseif (is_array($ownership)) {
            $ownershipIsIndexed = ArrayHelper::isIndexed($ownership);
            $user = ArrayHelper::getValue($ownership, $ownershipIsIndexed ? 0 : 'user');
            $group = ArrayHelper::getValue($ownership, $ownershipIsIndexed ? 1 : 'group');
        } else {
            throw new InvalidArgumentException('$ownership must be an integer, string, array, or null.');
        }
    }
    if ($mode !== null) {
        if (!is_int($mode)) {
            throw new InvalidArgumentException('$mode must be an integer or null.');
        }
        if (!chmod($path, $mode)) {
            throw new Exception('Unable to change mode of "' . $path . '" to "0' . decoct($mode) . '".');
        }
    }
    if ($user !== null && $user !== '') {
        if (is_numeric($user)) {
            $user = (int) $user;
        } elseif (!is_string($user)) {
            throw new InvalidArgumentException('The user part of $ownership must be an integer, string, or null.');
        }
        if (!chown($path, $user)) {
            throw new Exception('Unable to change user ownership of "' . $path . '" to "' . $user . '".');
        }
    }
    if ($group !== null && $group !== '') {
        if (is_numeric($group)) {
            $group = (int) $group;
        } elseif (!is_string($group)) {
            throw new InvalidArgumentException('The group part of $ownership must be an integer, string or null.');
        }
        if (!chgrp($path, $group)) {
            throw new Exception('Unable to change group ownership of "' . $path . '" to "' . $group . '".');
        }
    }
}

            
copyDirectory() public static method

定義於: yii\helpers\BaseFileHelper::copyDirectory()

將整個目錄複製為另一個目錄。

檔案和子目錄也將被複製過去。

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

來源目錄

$dst string

目的地目錄

$options array

目錄複製的選項。有效的選項包括

  • dirMode:整數,要為新複製的目錄設定的權限。預設為 0775。
  • fileMode: integer,用於設定新複製檔案的權限。預設值為目前的環境設定。
  • filter: callback,一個 PHP 回呼函式,會針對每個目錄或檔案呼叫。回呼函式的簽名應為:function ($path),其中 $path 指的是要篩選的完整路徑。回呼函式可以傳回下列其中一個值:

    • true:目錄或檔案將會被複製("only" 和 "except" 選項將會被忽略)
    • false:目錄或檔案將不會被複製("only" 和 "except" 選項將會被忽略)
    • null:"only" 和 "except" 選項將會決定是否應複製目錄或檔案
  • only: array,檔案路徑若要複製,應符合的模式列表。如果路徑的結尾包含模式字串,則路徑符合模式。例如,'.php' 符合所有以 '.php' 結尾的檔案路徑。請注意,模式中的 '/' 字元同時符合路徑中的 '/' 和 '\'。如果檔案路徑同時符合 "only" 和 "except" 中的模式,則不會被複製。
  • except: array,檔案或目錄若要排除複製,應符合的模式列表。如果路徑的結尾包含模式字串,則路徑符合模式。以 '/' 結尾的模式僅適用於目錄路徑,而不以 '/' 結尾的模式僅適用於檔案路徑。例如,'/a/b' 符合所有以 '/a/b' 結尾的檔案路徑;而 '.svn/' 符合以 '.svn' 結尾的目錄路徑。請注意,模式中的 '/' 字元同時符合路徑中的 '/' 和 '\'。
  • caseSensitive: boolean,"only" 或 "except" 中指定的模式是否應區分大小寫。預設值為 true。
  • recursive: boolean,是否也應複製子目錄下的檔案。預設值為 true。
  • beforeCopy: callback,一個 PHP 回呼函式,在複製每個子目錄或檔案之前呼叫。如果回呼函式傳回 false,則將取消該子目錄或檔案的複製操作。回呼函式的簽名應為:function ($from, $to),其中 $from 是要複製的子目錄或檔案來源,而 $to 是複製目標。
  • afterCopy: callback,一個 PHP 回呼函式,在每個子目錄或檔案成功複製後呼叫。回呼函式的簽名應為:function ($from, $to),其中 $from 是複製來源的子目錄或檔案,而 $to 是複製目標。
  • copyEmptyDirectories: boolean,是否複製空目錄。設定為 false 可避免建立不包含檔案的目錄。這會影響最初不包含檔案的目錄,以及由於檔案已透過 onlyexcept 篩選而導致目標位置不包含檔案的目錄。預設值為 true。此選項自 2.0.12 版本起可用。在 2.0.12 之前的版本中,總是會複製空目錄。
throws yii\base\InvalidArgumentException

如果無法開啟目錄

                public static function copyDirectory($src, $dst, $options = [])
{
    $src = static::normalizePath($src);
    $dst = static::normalizePath($dst);
    if ($src === $dst || strpos($dst, $src . DIRECTORY_SEPARATOR) === 0) {
        throw new InvalidArgumentException('Trying to copy a directory to itself or a subdirectory.');
    }
    $dstExists = is_dir($dst);
    if (!$dstExists && (!isset($options['copyEmptyDirectories']) || $options['copyEmptyDirectories'])) {
        static::createDirectory($dst, isset($options['dirMode']) ? $options['dirMode'] : 0775, true);
        $dstExists = true;
    }
    $handle = opendir($src);
    if ($handle === false) {
        throw new InvalidArgumentException("Unable to open directory: $src");
    }
    if (!isset($options['basePath'])) {
        // this should be done only once
        $options['basePath'] = realpath($src);
        $options = static::normalizeOptions($options);
    }
    while (($file = readdir($handle)) !== false) {
        if ($file === '.' || $file === '..') {
            continue;
        }
        $from = $src . DIRECTORY_SEPARATOR . $file;
        $to = $dst . DIRECTORY_SEPARATOR . $file;
        if (static::filterPath($from, $options)) {
            if (isset($options['beforeCopy']) && !call_user_func($options['beforeCopy'], $from, $to)) {
                continue;
            }
            if (is_file($from)) {
                if (!$dstExists) {
                    // delay creation of destination directory until the first file is copied to avoid creating empty directories
                    static::createDirectory($dst, isset($options['dirMode']) ? $options['dirMode'] : 0775, true);
                    $dstExists = true;
                }
                copy($from, $to);
                if (isset($options['fileMode'])) {
                    @chmod($to, $options['fileMode']);
                }
            } else {
                // recursive copy, defaults to true
                if (!isset($options['recursive']) || $options['recursive']) {
                    static::copyDirectory($from, $to, $options);
                }
            }
            if (isset($options['afterCopy'])) {
                call_user_func($options['afterCopy'], $from, $to);
            }
        }
    }
    closedir($handle);
}

            
createDirectory() public static method

Defined in: yii\helpers\BaseFileHelper::createDirectory()

建立新的目錄。

此方法與 PHP 的 mkdir() 函式類似,但它使用 chmod() 來設定所建立目錄的權限,以避免 umask 設定的影響。

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

要建立的目錄路徑。

$mode integer

要為建立的目錄設定的權限。

$recursive boolean

如果父目錄不存在,是否要建立父目錄。

return boolean

目錄是否成功建立

throws yii\base\Exception

如果目錄無法建立(例如:由於並行變更導致的 PHP 錯誤)

                public static function createDirectory($path, $mode = 0775, $recursive = true)
{
    if (is_dir($path)) {
        return true;
    }
    $parentDir = dirname($path);
    // recurse if parent dir does not exist and we are not at the root of the file system.
    if ($recursive && !is_dir($parentDir) && $parentDir !== $path) {
        static::createDirectory($parentDir, $mode, true);
    }
    try {
        if (!mkdir($path, $mode)) {
            return false;
        }
    } catch (\Exception $e) {
        if (!is_dir($path)) {// https://github.com/yiisoft/yii2/issues/9288
            throw new \yii\base\Exception("Failed to create directory \"$path\": " . $e->getMessage(), $e->getCode(), $e);
        }
    }
    try {
        return chmod($path, $mode);
    } catch (\Exception $e) {
        throw new \yii\base\Exception("Failed to change permissions for directory \"$path\": " . $e->getMessage(), $e->getCode(), $e);
    }
}

            
filterPath() public static method

Defined in: yii\helpers\BaseFileHelper::filterPath()

檢查給定的檔案路徑是否滿足篩選選項。

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

要檢查的檔案或目錄路徑

$options array

篩選選項。請參閱 findFiles() 以取得支援選項的說明。

return boolean

檔案或目錄是否符合篩選選項。

                public static function filterPath($path, $options)
{
    if (isset($options['filter'])) {
        $result = call_user_func($options['filter'], $path);
        if (is_bool($result)) {
            return $result;
        }
    }
    if (empty($options['except']) && empty($options['only'])) {
        return true;
    }
    $path = str_replace('\\', '/', $path);
    if (
        !empty($options['except'])
        && ($except = self::lastExcludeMatchingFromList($options['basePath'], $path, $options['except'])) !== null
    ) {
        return $except['flags'] & self::PATTERN_NEGATIVE;
    }
    if (!empty($options['only']) && !is_dir($path)) {
        return self::lastExcludeMatchingFromList($options['basePath'], $path, $options['only']) !== null;
    }
    return true;
}

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

Defined in: yii\helpers\BaseFileHelper::findDirectories()

傳回在指定目錄和子目錄下找到的目錄。

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

將在其中尋找檔案的目錄。

$options array

用於目錄搜尋的選項。有效選項為:

  • filter:callback,一個 PHP 回呼函式,會針對每個目錄或檔案呼叫。回呼函式的簽名應為:function (string $path): bool,其中 $path 指的是要篩選的完整路徑。回呼函式可以傳回下列其中一個值:

    • true:目錄將會被傳回
    • false:目錄將不會被傳回
  • recursive:boolean,是否也應尋找子目錄下的檔案。預設值為 true。請參閱 findFiles() 以取得更多選項。

return array

在目錄下找到的目錄,順序不一定。排序取決於使用的檔案系統。

throws yii\base\InvalidArgumentException

如果 dir 無效。

                public static function findDirectories($dir, $options = [])
{
    $dir = self::clearDir($dir);
    $options = self::setBasePath($dir, $options);
    $list = [];
    $handle = self::openDir($dir);
    while (($file = readdir($handle)) !== false) {
        if ($file === '.' || $file === '..') {
            continue;
        }
        $path = $dir . DIRECTORY_SEPARATOR . $file;
        if (is_dir($path) && static::filterPath($path, $options)) {
            $list[] = $path;
            if (!isset($options['recursive']) || $options['recursive']) {
                $list = array_merge($list, static::findDirectories($path, $options));
            }
        }
    }
    closedir($handle);
    return $list;
}

            
findFiles() public static method

Defined in: yii\helpers\BaseFileHelper::findFiles()

傳回在指定目錄和子目錄下找到的檔案。

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

將在其中尋找檔案的目錄。

$options array

用於檔案搜尋的選項。有效選項為:

  • filter:callback,一個 PHP 回呼函式,會針對每個目錄或檔案呼叫。回呼函式的簽名應為:function ($path),其中 $path 指的是要篩選的完整路徑。回呼函式可以傳回下列其中一個值:

    • true:目錄或檔案將會被傳回(onlyexcept 選項將會被忽略)
    • false:目錄或檔案將不會被傳回(onlyexcept 選項將會被忽略)
    • nullonlyexcept 選項將會決定是否應傳回目錄或檔案
  • except:array,從結果中排除的模式列表,用於比對檔案或目錄路徑。以斜線 ('/') 結尾的模式僅適用於目錄路徑,而不以 '/' 結尾的模式僅適用於檔案路徑。例如,'/a/b' 符合所有以 '/a/b' 結尾的檔案路徑;而 .svn/ 符合以 .svn 結尾的目錄路徑。如果模式不包含斜線 (/),則會將其視為 shell glob 模式,並檢查是否與相對於 $dir 的路徑名稱相符。否則,該模式將被視為適用於 fnmatch(3) 的 shell glob,並帶有 FNM_PATHNAME 標誌:模式中的萬用字元將不會比對路徑名稱中的 /。例如,views/*.php 符合 views/index.php,但不符合 views/controller/index.php。開頭的斜線符合路徑名稱的開頭。例如,/*.php 符合 index.php,但不符合 views/start/index.php。可選的前綴 ! 否定該模式;先前模式排除的任何符合檔案將再次包含在內。如果否定模式符合,這將覆蓋較低優先順序的模式來源。在以字面 ! 開頭的模式前面加上反斜線 (\),例如,\!important!.txt。請注意,模式中的 '/' 字元同時符合路徑中的 '/' 和 '\'。您可以在此處找到有關 gitignore 模式格式的更多詳細資訊。
  • only:array,檔案路徑若要傳回,應符合的模式列表。目錄路徑不會針對這些模式進行檢查。使用與 except 選項中相同的模式比對規則。如果檔案路徑同時符合 onlyexcept 中的模式,則不會被傳回。
  • caseSensitive:boolean,onlyexcept 中指定的模式是否應區分大小寫。預設值為 true
  • recursive:boolean,是否也應尋找子目錄下的檔案。預設值為 true
return array

在目錄下找到的檔案,順序不一定。排序取決於使用的檔案系統。

throws yii\base\InvalidArgumentException

如果 dir 無效。

                public static function findFiles($dir, $options = [])
{
    $dir = self::clearDir($dir);
    $options = self::setBasePath($dir, $options);
    $list = [];
    $handle = self::openDir($dir);
    while (($file = readdir($handle)) !== false) {
        if ($file === '.' || $file === '..') {
            continue;
        }
        $path = $dir . DIRECTORY_SEPARATOR . $file;
        if (static::filterPath($path, $options)) {
            if (is_file($path)) {
                $list[] = $path;
            } elseif (is_dir($path) && (!isset($options['recursive']) || $options['recursive'])) {
                $list = array_merge($list, static::findFiles($path, $options));
            }
        }
    }
    closedir($handle);
    return $list;
}

            
getExtensionByMimeType() public static method (available since version 2.0.48)

Defined in: yii\helpers\BaseFileHelper::getExtensionByMimeType()

根據給定的 MIME 類型判斷最常見的擴展名。

此方法將使用 MIME 類型與副檔名之間的本地對應。

public static string|null getExtensionByMimeType ( $mimeType, $preferShort false, $magicFile null )
$mimeType string

檔案 MIME 類型。

$preferShort boolean

傳回最多 3 個字元的副檔名。

$magicFile string|null

包含所有可用 MIME 類型資訊的檔案路徑(或別名)。如果未設定,將使用 $mimeMagicFile 指定的檔案。

return string|null

對應於指定 MIME 類型的副檔名

                public static function getExtensionByMimeType($mimeType, $preferShort = false, $magicFile = null)
{
    $aliases = static::loadMimeAliases(static::$mimeAliasesFile);
    if (isset($aliases[$mimeType])) {
        $mimeType = $aliases[$mimeType];
    }
    $mimeExtensions = static::loadMimeExtensions($magicFile);
    if (!array_key_exists($mimeType, $mimeExtensions)) {
        return null;
    }
    $extensions = $mimeExtensions[$mimeType];
    if (is_array($extensions)) {
        if ($preferShort) {
            foreach ($extensions as $extension) {
                if (mb_strlen($extension, 'UTF-8') <= 3) {
                    return $extension;
                }
            }
        }
        return $extensions[0];
    } else {
        return $extensions;
    }
}

            
getExtensionsByMimeType() public static method

Defined in: yii\helpers\BaseFileHelper::getExtensionsByMimeType()

根據給定的 MIME 類型判斷擴展名。

此方法將使用副檔名與 MIME 類型之間的本地對應。

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

檔案 MIME 類型。

$magicFile string|null

包含所有可用 MIME 類型資訊的檔案路徑(或別名)。如果未設定,將使用 $mimeMagicFile 指定的檔案。

return array

對應於指定 MIME 類型的副檔名

                public static function getExtensionsByMimeType($mimeType, $magicFile = null)
{
    $aliases = static::loadMimeAliases(static::$mimeAliasesFile);
    if (isset($aliases[$mimeType])) {
        $mimeType = $aliases[$mimeType];
    }
    // Note: For backwards compatibility the "MimeTypes" file is used.
    $mimeTypes = static::loadMimeTypes($magicFile);
    return array_keys($mimeTypes, mb_strtolower($mimeType, 'UTF-8'), true);
}

            
getMimeType() public static method

Defined in: yii\helpers\BaseFileHelper::getMimeType()

判斷指定檔案的 MIME 類型。

此方法將首先嘗試根據 finfo_open 判斷 MIME 類型。如果未安裝 fileinfo 擴充功能,當 $checkExtension 為 true 時,它將回退到 getMimeTypeByExtension()

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

檔案名稱。

$magicFile string|null

可選的 magic 資料庫檔案(或別名)名稱,通常類似 /path/to/magic.mime。當安裝 fileinfo 擴充功能時,這將作為第二個參數傳遞給 finfo_open()。如果 MIME 類型是透過 getMimeTypeByExtension() 判斷,且此參數為 null,則將使用 $mimeMagicFile 指定的檔案。

$checkExtension boolean

如果 finfo_open() 無法判斷 MIME 類型,是否使用檔案副檔名來判斷。

return string|null

MIME 類型(例如:text/plain)。如果無法判斷 MIME 類型,則傳回 Null。

throws yii\base\InvalidConfigException

當未安裝 fileinfo PHP 擴充功能且 $checkExtensionfalse 時。

                public static function getMimeType($file, $magicFile = null, $checkExtension = true)
{
    if ($magicFile !== null) {
        $magicFile = Yii::getAlias($magicFile);
    }
    if (!extension_loaded('fileinfo')) {
        if ($checkExtension) {
            return static::getMimeTypeByExtension($file, $magicFile);
        }
        throw new InvalidConfigException('The fileinfo PHP extension is not installed.');
    }
    $info = finfo_open(FILEINFO_MIME_TYPE, $magicFile);
    if ($info) {
        $result = finfo_file($info, $file);
        finfo_close($info);
        if ($result !== false) {
            return $result;
        }
    }
    return $checkExtension ? static::getMimeTypeByExtension($file, $magicFile) : null;
}

            
getMimeTypeByExtension() public static method

Defined in: yii\helpers\BaseFileHelper::getMimeTypeByExtension()

根據指定檔案的擴展名判斷 MIME 類型。

此方法將使用副檔名與 MIME 類型之間的本地對應。

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

檔案名稱。

$magicFile string|null

包含所有可用 MIME 類型資訊的檔案路徑(或別名)。如果未設定,將使用 $mimeMagicFile 指定的檔案。

return string|null

MIME 類型。如果無法判斷 MIME 類型,則傳回 Null。

                public static function getMimeTypeByExtension($file, $magicFile = null)
{
    $mimeTypes = static::loadMimeTypes($magicFile);
    if (($ext = pathinfo($file, PATHINFO_EXTENSION)) !== '') {
        $ext = strtolower($ext);
        if (isset($mimeTypes[$ext])) {
            return $mimeTypes[$ext];
        }
    }
    return null;
}

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

Defined in: yii\helpers\BaseFileHelper::loadMimeAliases()

從指定的檔案載入 MIME 別名。

protected static array loadMimeAliases ( $aliasesFile )
$aliasesFile string|null

包含 MIME 類型別名的檔案路徑(或別名)。如果未設定,將使用 $mimeAliasesFile 指定的檔案。

return array

從檔案副檔名到 MIME 類型的對應

                protected static function loadMimeAliases($aliasesFile)
{
    if ($aliasesFile === null) {
        $aliasesFile = static::$mimeAliasesFile;
    }
    $aliasesFile = Yii::getAlias($aliasesFile);
    if (!isset(self::$_mimeAliases[$aliasesFile])) {
        self::$_mimeAliases[$aliasesFile] = require $aliasesFile;
    }
    return self::$_mimeAliases[$aliasesFile];
}

            
loadMimeExtensions() protected static method (available since version 2.0.48)

Defined in: yii\helpers\BaseFileHelper::loadMimeExtensions()

從指定的檔案載入 MIME 擴展名。

protected static array loadMimeExtensions ( $extensionsFile )
$extensionsFile string|null

包含 MIME 類型別名的檔案路徑(或別名)。如果未設定,將使用 $mimeAliasesFile 指定的檔案。

return array

從檔案副檔名到 MIME 類型的對應

                protected static function loadMimeExtensions($extensionsFile)
{
    if ($extensionsFile === null) {
        $extensionsFile = static::$mimeExtensionsFile;
    }
    $extensionsFile = Yii::getAlias($extensionsFile);
    if (!isset(self::$_mimeExtensions[$extensionsFile])) {
        self::$_mimeExtensions[$extensionsFile] = require $extensionsFile;
    }
    return self::$_mimeExtensions[$extensionsFile];
}

            
loadMimeTypes() protected static method

Defined in: yii\helpers\BaseFileHelper::loadMimeTypes()

從指定的檔案載入 MIME 類型。

protected static array loadMimeTypes ( $magicFile )
$magicFile string|null

包含所有可用 MIME 類型資訊的檔案路徑(或別名)。如果未設定,將使用 $mimeMagicFile 指定的檔案。

return array

從檔案副檔名到 MIME 類型的對應

                protected static function loadMimeTypes($magicFile)
{
    if ($magicFile === null) {
        $magicFile = static::$mimeMagicFile;
    }
    $magicFile = Yii::getAlias($magicFile);
    if (!isset(self::$_mimeTypes[$magicFile])) {
        self::$_mimeTypes[$magicFile] = require $magicFile;
    }
    return self::$_mimeTypes[$magicFile];
}

            
localize() public static method

Defined in: yii\helpers\BaseFileHelper::localize()

傳回指定檔案的本地化版本。

搜尋基於指定的語言代碼。特別是,將在子目錄下尋找具有相同名稱的檔案,該子目錄的名稱與語言代碼相同。例如,給定檔案 "path/to/view.php" 和語言代碼 "zh-CN",將在 "path/to/zh-CN/view.php" 中尋找本地化檔案。如果找不到該檔案,它將嘗試回退到僅使用語言代碼 "zh",即 "path/to/zh/view.php"。如果仍然找不到,則將傳回原始檔案。

如果目標語言代碼和來源語言代碼相同,則將傳回原始檔案。

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

原始檔案

$language string|null

檔案應本地化為的目標語言。如果未設定,將使用 yii\base\Application::$language 的值。

$sourceLanguage string|null

原始檔案所使用的語言。如果未設定,將使用 yii\base\Application::$sourceLanguage 的值。

return string

符合的本地化檔案,如果找不到本地化版本,則為原始檔案。如果目標語言代碼和來源語言代碼相同,則將傳回原始檔案。

                public static function localize($file, $language = null, $sourceLanguage = null)
{
    if ($language === null) {
        $language = Yii::$app->language;
    }
    if ($sourceLanguage === null) {
        $sourceLanguage = Yii::$app->sourceLanguage;
    }
    if ($language === $sourceLanguage) {
        return $file;
    }
    $desiredFile = dirname($file) . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . basename($file);
    if (is_file($desiredFile)) {
        return $desiredFile;
    }
    $language = substr($language, 0, 2);
    if ($language === $sourceLanguage) {
        return $file;
    }
    $desiredFile = dirname($file) . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . basename($file);
    return is_file($desiredFile) ? $desiredFile : $file;
}

            
normalizeOptions() protected static method (available since version 2.0.12)
protected static array normalizeOptions ( array $options )
$options array

原始選項

return array

標準化選項

                protected static function normalizeOptions(array $options)
{
    if (!array_key_exists('caseSensitive', $options)) {
        $options['caseSensitive'] = true;
    }
    if (isset($options['except'])) {
        foreach ($options['except'] as $key => $value) {
            if (is_string($value)) {
                $options['except'][$key] = self::parseExcludePattern($value, $options['caseSensitive']);
            }
        }
    }
    if (isset($options['only'])) {
        foreach ($options['only'] as $key => $value) {
            if (is_string($value)) {
                $options['only'][$key] = self::parseExcludePattern($value, $options['caseSensitive']);
            }
        }
    }
    return $options;
}

            
normalizePath() public static method

Defined in: yii\helpers\BaseFileHelper::normalizePath()

標準化檔案/目錄路徑。

標準化會執行以下工作:

  • 將所有目錄分隔符號轉換為 DIRECTORY_SEPARATOR(例如:"\a/b\c" 變成 "/a/b/c")
  • 移除尾部的目錄分隔符號(例如:"/a/b/c/" 變成 "/a/b/c")
  • 將多個連續的斜線變成單個斜線(例如:"/a///b/c" 變成 "/a/b/c")
  • 根據 ".." 和 "." 的含義移除它們(例如:"/a/./b/../c" 變成 "/a/c")

注意:對於已註冊的串流包裝器,將跳過連續斜線規則和 ".."/"." 轉換。

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

要標準化的檔案/目錄路徑

$ds string

要在標準化結果中使用的目錄分隔符號。預設值為 DIRECTORY_SEPARATOR

return string

標準化的檔案/目錄路徑

                public static function normalizePath($path, $ds = DIRECTORY_SEPARATOR)
{
    $path = rtrim(strtr($path, '/\\', $ds . $ds), $ds);
    if (strpos($ds . $path, "{$ds}.") === false && strpos($path, "{$ds}{$ds}") === false) {
        return $path;
    }
    // fix #17235 stream wrappers
    foreach (stream_get_wrappers() as $protocol) {
        if (strpos($path, "{$protocol}://") === 0) {
            return $path;
        }
    }
    // the path may contain ".", ".." or double slashes, need to clean them up
    if (strpos($path, "{$ds}{$ds}") === 0 && $ds == '\\') {
        $parts = [$ds];
    } else {
        $parts = [];
    }
    foreach (explode($ds, $path) as $part) {
        if ($part === '..' && !empty($parts) && end($parts) !== '..') {
            array_pop($parts);
        } elseif ($part === '.' || $part === '' && !empty($parts)) {
            continue;
        } else {
            $parts[] = $part;
        }
    }
    $path = implode($ds, $parts);
    return $path === '' ? '.' : $path;
}

            
removeDirectory() public static method

Defined in: yii\helpers\BaseFileHelper::removeDirectory()

遞迴地移除目錄(及其所有內容)。

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

要遞迴刪除的目錄。

$options array

用於目錄移除的選項。有效選項為:

  • traverseSymlinks: boolean,是否也應遍歷目錄的符號連結。預設值為 false,表示不會刪除符號連結目錄的內容。在該預設情況下,只會移除符號連結。
throws yii\base\ErrorException

在發生錯誤時

                public static function removeDirectory($dir, $options = [])
{
    if (!is_dir($dir)) {
        return;
    }
    if (!empty($options['traverseSymlinks']) || !is_link($dir)) {
        if (!($handle = opendir($dir))) {
            return;
        }
        while (($file = readdir($handle)) !== false) {
            if ($file === '.' || $file === '..') {
                continue;
            }
            $path = $dir . DIRECTORY_SEPARATOR . $file;
            if (is_dir($path)) {
                static::removeDirectory($path, $options);
            } else {
                static::unlink($path);
            }
        }
        closedir($handle);
    }
    if (is_link($dir)) {
        static::unlink($dir);
    } else {
        rmdir($dir);
    }
}

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

Defined in: yii\helpers\BaseFileHelper::unlink()

以跨平台的方式移除檔案或符號連結

public static boolean unlink ( $path )
$path string