類別 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 |
屬性詳細資訊
包含 MIME 別名的 PHP 檔案路徑(或別名)。
包含每個 MIME 類型的擴展名的 PHP 檔案路徑(或別名)。
包含 MIME 類型資訊的 PHP 檔案路徑(或別名)。
方法詳細資訊
變更檔案或目錄的 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],例如 |
$mode | integer|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 . '".');
}
}
}
將整個目錄複製為另一個目錄。
檔案和子目錄也將被複製過去。
public static void copyDirectory ( $src, $dst, $options = [] ) | ||
$src | 字串 |
來源目錄 |
$dst | 字串 |
目的地目錄 |
$options | array |
目錄複製的選項。有效的選項為
|
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);
}
建立新目錄。
此方法與 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);
}
}
檢查給定的檔案路徑是否滿足篩選選項。
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;
}
傳回在指定目錄和子目錄下找到的目錄。
public static array findDirectories ( $dir, $options = [] ) | ||
$dir | 字串 |
將在其中尋找檔案的目錄。 |
$options | array |
目錄搜尋的選項。有效的選項為
|
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;
}
傳回在指定目錄和子目錄下找到的檔案。
public static array findFiles ( $dir, $options = [] ) | ||
$dir | 字串 |
將在其中尋找檔案的目錄。 |
$options | array |
檔案搜尋的選項。有效的選項為
|
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;
}
根據給定的 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;
}
}
根據給定的 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);
}
判斷指定檔案的 MIME 類型。
此方法將首先嘗試根據 finfo_open 判斷 MIME 類型。如果未安裝 fileinfo
擴充功能,則當 $checkExtension
為 true 時,它將回退到 getMimeTypeByExtension()。
public static string|null getMimeType ( $file, $magicFile = null, $checkExtension = true ) | ||
$file | 字串 |
檔案名稱。 |
$magicFile | string|null |
可選的 magic 資料庫檔案(或別名)的名稱,通常類似 |
$checkExtension | boolean |
如果 |
return | string|null |
MIME 類型(例如 |
---|---|---|
throws | yii\base\InvalidConfigException |
當未安裝 |
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;
}
根據指定檔案的擴展名判斷 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;
}
從指定檔案載入 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];
}
從指定檔案載入 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];
}
從指定檔案載入 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];
}
傳回指定檔案的本地化版本。
搜尋基於指定的語言代碼。特別是,將在子目錄下尋找具有相同名稱的檔案,該子目錄的名稱與語言代碼相同。例如,給定檔案 "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;
}
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;
}
正規化檔案/目錄路徑。
正規化會執行以下工作
- 將所有目錄分隔符號轉換為
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 | 字串 |
要在正規化結果中使用的目錄分隔符號。預設為 |
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;
}
遞迴移除目錄(及其所有內容)。
public static void removeDirectory ( $dir, $options = [] ) | ||
$dir | 字串 |
要遞迴刪除的目錄。 |
$options | array |
目錄移除的選項。有效的選項為
|
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);
}
}
以跨平台方式移除檔案或符號連結
public static boolean unlink ( $path ) | ||
$path | 字串 |
public static function unlink($path)
{
$isWindows = DIRECTORY_SEPARATOR === '\\';
if (!$isWindows) {
return unlink($path);
}
if (is_link($path) && is_dir($path)) {
return rmdir($path);
}
try {
return unlink($path);
} catch (ErrorException $e) {
// last resort measure for Windows
if (is_dir($path) && count(static::findFiles($path)) !== 0) {
return false;
}
if (function_exists('exec') && file_exists($path)) {
exec('DEL /F/Q ' . escapeshellarg($path));
return !file_exists($path);
}
return false;
}
}
註冊 或 登入 以便留言。