クラス 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 | string | MIME エイリアスを含む PHP ファイルのパス (またはエイリアス)。 | yii\helpers\BaseFileHelper |
$mimeExtensionsFile | string | MIME タイプごとの拡張子を含む PHP ファイルのパス (またはエイリアス)。 | yii\helpers\BaseFileHelper |
$mimeMagicFile | string | MIME タイプ情報を含む PHP ファイルのパス (またはエイリアス)。 | 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 | string |
ファイルまたはディレクトリへのパス。 |
$ownership | string|array|integer|null |
ファイルまたはディレクトリのユーザーおよび/またはグループの所有権。$ownership が文字列の場合、形式は 'user:group' で、両方ともオプションです。例えば、'user' または 'user:' はユーザーのみを変更し、':group' はグループのみを変更し、'user:group' は両方を変更します。$owners がインデックス配列の場合、形式は [0 => user, 1 => group] です。例えば、 |
$mode | 整数|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 | string |
コピー元のディレクトリ |
$dst | string |
コピー先のディレクトリ |
$options | 配列 |
ディレクトリコピーのオプション。有効なオプションは以下のとおりです。
|
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()
関数と似ていますが、umask
設定の影響を避けるために、作成されたディレクトリのパーミッションを設定するために chmod()
を使用します。
public static boolean createDirectory ( $path, $mode = 0775, $recursive = true ) | ||
$path | string |
作成されるディレクトリのパス。 |
$mode | 整数 |
作成されたディレクトリに設定されるパーミッション。 |
$recursive | ブール値 |
親ディレクトリが存在しない場合に作成するかどうか。 |
return | ブール値 |
ディレクトリが正常に作成されたかどうか |
---|---|---|
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 | string |
チェックされるファイルまたはディレクトリのパス |
$options | 配列 |
フィルタリングオプション。サポートされるオプションの説明については、findFiles() を参照してください。 |
return | ブール値 |
ファイルまたはディレクトリがフィルタリングオプションを満たしているかどうか。 |
---|
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 | string |
ファイルが検索されるディレクトリ。 |
$options | 配列 |
ディレクトリ検索のオプション。有効なオプションは以下のとおりです。
|
return | 配列 |
ディレクトリの下にあるディレクトリ。特定の順序はありません。順序は使用されるファイルシステムによって異なります。 |
---|---|---|
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 | string |
ファイルが検索されるディレクトリ。 |
$options | 配列 |
ファイル検索のオプション。有効なオプションは以下のとおりです。
|
return | 配列 |
ディレクトリで見つかったファイル。特定の順序はありません。順序は使用されるファイルシステムに依存します。 |
---|---|---|
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 | string |
ファイルのMIMEタイプ。 |
$preferShort | ブール値 |
最大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 | string |
ファイルのMIMEタイプ。 |
$magicFile | string|null |
利用可能なすべてのMIMEタイプ情報を含むファイルのパス(またはエイリアス)。これが設定されていない場合は、$mimeMagicFile で指定されたファイルが使用されます。 |
return | 配列 |
指定された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 | string |
ファイル名。 |
$magicFile | string|null |
オプションのmagicデータベースファイル(またはエイリアス)の名前。通常は |
$checkExtension | ブール値 |
|
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 | 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;
}
指定されたファイルから MIME エイリアスを読み込みます。
protected static array loadMimeAliases ( $aliasesFile ) | ||
$aliasesFile | string|null |
MIMEタイプのエイリアスを含むファイルのパス(またはエイリアス)。これが設定されていない場合は、$mimeAliasesFile で指定されたファイルが使用されます。 |
return | 配列 |
ファイル拡張子から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 | 配列 |
ファイル拡張子から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 | 配列 |
ファイル拡張子から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 | 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;
}
protected static array normalizeOptions ( array $options ) | ||
$options | 配列 |
生のオプション |
return | 配列 |
正規化されたオプション |
---|
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」になります)
- 複数の連続したスラッシュを1つに変換します(例:「/a///b/c」は「/a/b/c」になります)
- 「..」と「.」を意味に基づいて削除します(例:「/a/./b/../c」は「/a/c」になります)
注:登録済みのストリームラッパーの場合、連続したスラッシュルールと「..」/「.」の変換はスキップされます。
public static string normalizePath ( $path, $ds = DIRECTORY_SEPARATOR ) | ||
$path | string |
正規化されるファイル/ディレクトリのパス |
$ds | string |
正規化された結果で使用されるディレクトリ区切り文字。デフォルトは |
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;
}
ディレクトリ (およびそのすべての内容) を再帰的に削除します。
public static void removeDirectory ( $dir, $options = [] ) | ||
$dir | string |
再帰的に削除されるディレクトリ。 |
$options | 配列 |
ディレクトリ削除のオプション。有効なオプションは次のとおりです。
|
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 | string |
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;
}
}
サインアップ または ログイン してコメントしてください。