0 關注者

類別 yii\helpers\BaseFileHelper

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

BaseFileHelper 為 yii\helpers\FileHelper 提供具體實作。

請勿使用 BaseFileHelper。請改用 yii\helpers\FileHelper

公開屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$mimeAliasesFile 字串 包含 MIME 別名的 PHP 檔案路徑(或別名)。 yii\helpers\BaseFileHelper
$mimeExtensionsFile 字串 包含每個 MIME 類型的擴展名的 PHP 檔案路徑(或別名)。 yii\helpers\BaseFileHelper
$mimeMagicFile 字串 包含 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

屬性詳細資訊

隱藏繼承的屬性

$mimeAliasesFile public static property (自 2.0.14 版本起可用)

包含 MIME 別名的 PHP 檔案路徑(或別名)。

public static string $mimeAliasesFile '@yii/helpers/mimeAliases.php'
$mimeExtensionsFile public static property (自 2.0.48 版本起可用)

包含每個 MIME 類型的擴展名的 PHP 檔案路徑(或別名)。

public static string $mimeExtensionsFile '@yii/helpers/mimeExtensions.php'
$mimeMagicFile public static property

包含 MIME 類型資訊的 PHP 檔案路徑(或別名)。

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

方法詳細資訊

隱藏繼承的方法

changeOwnership() public static method (available since version 2.0.43)

變更檔案或目錄的 Unix 使用者和/或群組擁有權,並可選擇性地變更模式。

注意:此函數不適用於遠端檔案,因為要檢查的檔案必須可透過伺服器的檔案系統存取。注意:在 Windows 上,當應用於一般檔案時,此函數會靜默失敗。

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

檔案或目錄的路徑。

$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

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

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

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

來源目錄

$dst 字串

目的地目錄

$options array

目錄複製的選項。有效的選項為

  • dirMode: integer,為新複製的目錄設定的權限。預設為 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

建立新目錄。

此方法與 PHP 的 mkdir() 函數類似,不同之處在於它使用 chmod() 來設定已建立目錄的權限,以避免 umask 設定的影響。

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

要建立的目錄路徑。

$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

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

public static boolean filterPath ( $path, $options )
$path 字串

要檢查的檔案或目錄的路徑

$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)

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

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

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

$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

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

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

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

$options array

檔案搜尋的選項。有效的選項為

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

    • true: 將傳回目錄或檔案(將忽略 onlyexcept 選項)
    • false: 將 *不* 傳回目錄或檔案(將忽略 onlyexcept 選項)
    • null: onlyexcept 選項將決定是否應傳回目錄或檔案
  • 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)

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

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

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

檔案 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

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

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

public static array getExtensionsByMimeType ( $mimeType, $magicFile null )
$mimeType 字串

檔案 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

判斷指定檔案的 MIME 類型。

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

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

檔案名稱。

$magicFile string|null

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

$checkExtension boolean

如果 finfo_open() 無法判斷 MIME 類型,是否使用檔案副檔名來判斷 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

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

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

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

檔案名稱。

$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)

從指定檔案載入 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)

從指定檔案載入 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

從指定檔案載入 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

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

搜尋基於指定的語言代碼。特別是,將在子目錄下尋找具有相同名稱的檔案,該子目錄的名稱與語言代碼相同。例如,給定檔案 "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 字串

原始檔案

$language string|null

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

$sourceLanguage string|null

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

return 字串

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

                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

正規化檔案/目錄路徑。

正規化會執行以下工作

  • 將所有目錄分隔符號轉換為 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 字串

要正規化的檔案/目錄路徑

$ds 字串

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

return 字串

正規化的檔案/目錄路徑

                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

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

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

要遞迴刪除的目錄。

$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)

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

public static boolean unlink ( $path )
$path 字串