|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace luya\helpers; |
|
4
|
|
|
|
|
5
|
|
|
use admin\ngrest\plugins\File; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Extending the Yii File Helper class. |
|
10
|
|
|
* |
|
11
|
|
|
* @author nadar |
|
12
|
|
|
*/ |
|
13
|
|
|
class FileHelper extends \yii\helpers\BaseFileHelper |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Generate a human readable size informations from provided Byte size |
|
17
|
|
|
* |
|
18
|
|
|
* @param integer $size The size to convert in Byte |
|
19
|
|
|
* @return string The readable size definition |
|
20
|
|
|
*/ |
|
21
|
|
|
public static function humanReadableFilesize($size) |
|
22
|
|
|
{ |
|
23
|
|
|
$mod = 1024; |
|
24
|
|
|
$units = explode(' ', 'B KB MB GB TB PB'); |
|
25
|
|
|
for ($i = 0; $size > $mod; ++$i) { |
|
26
|
|
|
$size /= $mod; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return round($size, 2).' '.$units[$i]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Append a file extension to a path/file if there is no or an empty extension provided, this |
|
34
|
|
|
* helper methods is used to make sure the right extension existing on files. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $file The file where extension should be append if not existing |
|
37
|
|
|
* @param string $extension |
|
38
|
|
|
* @return the ensured file/path with extension |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function ensureExtension($file, $extension) |
|
41
|
|
|
{ |
|
42
|
|
|
$info = pathinfo($file); |
|
43
|
|
|
if (!isset($info['extension']) || empty($info['extension'])) { |
|
44
|
|
|
$file = rtrim($file, '.') . '.' . $extension; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $file; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get extension and name from a file for the provided source/path of the file. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $sourceFile The path of the file |
|
54
|
|
|
* @return object With extension and name keys. |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function getFileInfo($sourceFile) |
|
57
|
|
|
{ |
|
58
|
|
|
$path = pathinfo($sourceFile); |
|
59
|
|
|
|
|
60
|
|
|
return (object) [ |
|
61
|
|
|
'extension' => isset($path['extension']) ? (empty($path['extension']) ? false : $path['extension']) : false, |
|
62
|
|
|
'name' => isset($path['filename']) ? $path['filename'] : false, |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Generate a md5 hash of a file. This is eqauls to `md5sum` command |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $sourceFile The path to the file |
|
70
|
|
|
* @return false|string Returns false or the md5 hash of this file |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function getFileHash($sourceFile) |
|
73
|
|
|
{ |
|
74
|
|
|
return file_exists($sourceFile) ? hash_file('md5', $sourceFile) : false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Basic helper method to write files with exception capture |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $fileName The path to the file with file name |
|
81
|
|
|
* @param string $content The content to store in this File |
|
82
|
|
|
* @return boolean |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function writeFile($fileName, $content) |
|
85
|
|
|
{ |
|
86
|
|
|
try { |
|
87
|
|
|
return file_put_contents($fileName, $content); |
|
88
|
|
|
} catch (Exception $error) { |
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|