Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
b8354224
Commit
b8354224
authored
Sep 10, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #4897: Added `yii\helpers\FileHelper::mimeMagicFile
parent
353cf1b3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
11 deletions
+22
-11
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
BaseFileHelper.php
framework/helpers/BaseFileHelper.php
+21
-11
No files found.
framework/CHANGELOG.md
View file @
b8354224
...
...
@@ -202,6 +202,7 @@ Yii Framework 2 Change Log
-
Enh #4062: Added 'caseSensitive' option to
`yii\helpers\BaseFileHelper::findFiles()`
(klimov-paul)
-
Enh #4691: Encoding on
`ActiveForm`
and
`ActiveField`
validation errors is now configurable (Alex-Code)
-
Enh #4740: Added
`yii\web\Session::addFlash()`
(restyler)
-
Enh #4897: Added
`yii\helpers\FileHelper::mimeMagicFile`
(qiangxue)
-
Enh: Added support for using sub-queries when building a DB query with
`IN`
condition (qiangxue)
-
Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue)
-
Enh: Added
`yii\web\UrlManager::addRules()`
to simplify adding new URL rules (qiangxue)
...
...
framework/helpers/BaseFileHelper.php
View file @
b8354224
...
...
@@ -28,6 +28,10 @@ class BaseFileHelper
const
PATTERN_NEGATIVE
=
16
;
const
PATTERN_CASE_INSENSITIVE
=
32
;
/**
* @var string the path (or alias) of a PHP file containing MIME type information.
*/
public
static
$mimeMagicFile
=
'@yii/helpers/mimeTypes.php'
;
/**
* Normalizes a file/directory path.
...
...
@@ -112,11 +116,13 @@ class BaseFileHelper
/**
* Determines the MIME type of the specified file.
* This method will first try to determine the MIME type based on
* [finfo_open](http://php.net/manual/en/function.finfo-open.php). If th
is doesn't work, it will
*
fall back to [[getMimeTypeByExtension()]]
.
* [finfo_open](http://php.net/manual/en/function.finfo-open.php). If th
e `fileinfo` extension is not installed,
*
it will fall back to [[getMimeTypeByExtension()]] when `$checkExtension` is true
.
* @param string $file the file name.
* @param string $magicFile name of the optional magic database file, usually something like `/path/to/magic.mime`.
* This will be passed as the second parameter to [finfo_open](http://php.net/manual/en/function.finfo-open.php).
* @param string $magicFile name of the optional magic database file (or alias), usually something like `/path/to/magic.mime`.
* This will be passed as the second parameter to [finfo_open()](http://php.net/manual/en/function.finfo-open.php)
* when the `fileinfo` extension is installed. If the MIME type is being determined based via [[getMimeTypeByExtension()]]
* and this is null, it will use the file specified by [[mimeMagicFile]].
* @param boolean $checkExtension whether to use the file extension to determine the MIME type in case
* `finfo_open()` cannot determine it.
* @return string the MIME type (e.g. `text/plain`). Null is returned if the MIME type cannot be determined.
...
...
@@ -124,6 +130,9 @@ class BaseFileHelper
*/
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
);
...
...
@@ -149,8 +158,8 @@ class BaseFileHelper
* Determines the MIME type based on the extension name of the specified file.
* This method will use a local map between extension names and MIME types.
* @param string $file the file name.
* @param string $magicFile the path of the file that contains all available MIME type information.
* If this is not set, the
default file aliased by `@yii/helpers/mimeTypes.php`
will be used.
* @param string $magicFile the path
(or alias)
of the file that contains all available MIME type information.
* If this is not set, the
file specified by [[mimeMagicFile]]
will be used.
* @return string the MIME type. Null is returned if the MIME type cannot be determined.
*/
public
static
function
getMimeTypeByExtension
(
$file
,
$magicFile
=
null
)
...
...
@@ -171,8 +180,8 @@ class BaseFileHelper
* Determines the extensions by given MIME type.
* This method will use a local map between extension names and MIME types.
* @param string $mimeType file MIME type.
* @param string $magicFile the path of the file that contains all available MIME type information.
* If this is not set, the
default file aliased by `@yii/helpers/mimeTypes.php`
will be used.
* @param string $magicFile the path
(or alias)
of the file that contains all available MIME type information.
* If this is not set, the
file specified by [[mimeMagicFile]]
will be used.
* @return array the extensions corresponding to the specified MIME type
*/
public
static
function
getExtensionsByMimeType
(
$mimeType
,
$magicFile
=
null
)
...
...
@@ -185,15 +194,16 @@ class BaseFileHelper
/**
* Loads MIME types from the specified file.
* @param string $magicFile the
file that contains
MIME type information.
* If
null, the file `@yii/helpers/mimeTypes.php`
will be used.
* @param string $magicFile the
path (or alias) of the file that contains all available
MIME type information.
* If
this is not set, the file specified by [[mimeMagicFile]]
will be used.
* @return array the mapping from file extensions to MIME types
*/
protected
static
function
loadMimeTypes
(
$magicFile
)
{
if
(
$magicFile
===
null
)
{
$magicFile
=
__DIR__
.
'/mimeTypes.php'
;
$magicFile
=
static
::
$mimeMagicFile
;
}
$magicFile
=
Yii
::
getAlias
(
$magicFile
);
if
(
!
isset
(
self
::
$_mimeTypes
[
$magicFile
]))
{
self
::
$_mimeTypes
[
$magicFile
]
=
require
(
$magicFile
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment