1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* HiPanel core package |
4
|
|
|
* |
5
|
|
|
* @link https://hipanel.com/ |
6
|
|
|
* @package hipanel-core |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\helpers; |
12
|
|
|
|
13
|
|
|
use Yii; |
14
|
|
|
use yii\base\InvalidParamException; |
15
|
|
|
|
16
|
|
|
class FileHelper extends \yii\helpers\FileHelper |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Returns the MIME-type of content in string. |
20
|
|
|
* @param string $content |
21
|
|
|
* @return string Content mime-type |
22
|
|
|
*/ |
23
|
|
|
public static function getContentMimeType($content) |
24
|
|
|
{ |
25
|
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE); |
26
|
|
|
$mimeType = finfo_buffer($finfo, $content); |
27
|
|
|
finfo_close($finfo); |
28
|
|
|
|
29
|
|
|
return $mimeType; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Builds path to the file $filename under the $directory with additional sub-directories. |
34
|
|
|
* For example, the $directory is `/var/log/yii`, the $filename is `f0527.jpg`. |
35
|
|
|
* Calling this method without passing optional parameters, will generate the following path: |
36
|
|
|
* `/var/log/yii/f0/f0527.jpg`. |
37
|
|
|
* Setting $nests to `2`, the path will look like `/var/log/yii/f0/52/f0527.jpg` |
38
|
|
|
* Setting $nests to `3` and $length to `1` you will get `/var/log/yii/f/0/5/f0527.jpg`. |
39
|
|
|
* |
40
|
|
|
* It is strongly recommended to pass only hashes as $filename in order be sure that length is enough. |
41
|
|
|
* |
42
|
|
|
* @param string $directory Path to the base directory |
43
|
|
|
* @param string $filename The file name |
44
|
|
|
* @param int $nests Number of nested directories |
45
|
|
|
* @param int $length Length of the nested directory name |
46
|
|
|
* @throws InvalidParamException filename does not contain enough characters for directory nesting |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public static function getPrefixedPath($directory, $filename, $nests = 1, $length = 2) |
50
|
|
|
{ |
51
|
|
|
if (strlen(basename($filename)) < $nests * $length) { |
52
|
|
|
throw new InvalidParamException('Filename does not contain enough characters for directory nesting'); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
for ($start = 0; $start < $nests * $length; $start += $length) { |
56
|
|
|
$level = substr($filename, $start, $length); |
57
|
|
|
$directory .= DIRECTORY_SEPARATOR . $level; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$directory .= DIRECTORY_SEPARATOR . $filename; |
61
|
|
|
|
62
|
|
|
return Yii::getAlias($directory); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.