|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.newicon.net/neon |
|
4
|
|
|
* @copyright Copyright (c) 2008 Newicon Ltd |
|
5
|
|
|
* @license http://www.newicon.net/neon/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace neon\core\helpers; |
|
9
|
|
|
|
|
10
|
|
|
use \neon\core\helpers\Str; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class FileHelper |
|
14
|
|
|
* @author Steve O'Brien - Newicon Ltd |
|
15
|
|
|
* @package neon\core\helpers |
|
16
|
|
|
*/ |
|
17
|
|
|
class File extends \yii\helpers\FileHelper |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Return the number in bytes representing the max upload size. |
|
21
|
|
|
* Considers both the PHP post max size and the PHP upload max size. |
|
22
|
|
|
* Requires the ini_get function |
|
23
|
|
|
* |
|
24
|
|
|
* @return int |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function maxUploadSize() |
|
27
|
|
|
{ |
|
28
|
|
|
$postMax = Str::toBytes(ini_get('post_max_size')); |
|
29
|
|
|
$uploadMax = Str::toBytes(ini_get('upload_max_filesize')); |
|
30
|
|
|
return min($postMax, $uploadMax); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Return a nicely formatted string representing the max upload size |
|
35
|
|
|
* |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function maxUploadSizeString() |
|
39
|
|
|
{ |
|
40
|
|
|
return Str::formatBytes(self::maxUploadSize()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create a file at a give path or alias path |
|
45
|
|
|
* This function is similar to file_put_contents however it will ensure the nested directory path exists |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $path |
|
48
|
|
|
* @param string $content |
|
49
|
|
|
* @throws \yii\base\Exception if the directory could not be created (i.e. php error due to parallel changes) |
|
50
|
|
|
* @return bool|int - the number of bytes written to the file, or false on failure |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function createFile($path, $content) |
|
53
|
|
|
{ |
|
54
|
|
|
static::createDirectory(dirname($path)); |
|
55
|
|
|
return file_put_contents($path, $content); |
|
56
|
|
|
} |
|
57
|
|
|
} |