Issues (3627)

app/bundles/CoreBundle/Helper/FileHelper.php (1 issue)

1
<?php
2
3
/**
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @see         http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CoreBundle\Helper;
13
14
class FileHelper
15
{
16
    const BYTES_TO_MEGABYTES_RATIO = 1048576;
17
18
    public static function convertBytesToMegabytes($b)
19
    {
20
        return round($b / self::BYTES_TO_MEGABYTES_RATIO, 2);
21
    }
22
23
    public static function convertMegabytesToBytes($mb)
24
    {
25
        return $mb * self::BYTES_TO_MEGABYTES_RATIO;
26
    }
27
28
    public static function getMaxUploadSizeInBytes()
29
    {
30
        $maxPostSize   = self::convertPHPSizeToBytes(ini_get('post_max_size'));
31
        $maxUploadSize = self::convertPHPSizeToBytes(ini_get('upload_max_filesize'));
32
        $memoryLimit   = self::convertPHPSizeToBytes(ini_get('memory_limit'));
33
34
        return min($maxPostSize, $maxUploadSize, $memoryLimit);
35
    }
36
37
    public static function getMaxUploadSizeInMegabytes()
38
    {
39
        $maxUploadSizeInBytes = self::getMaxUploadSizeInBytes();
40
41
        return self::convertBytesToMegabytes($maxUploadSizeInBytes);
42
    }
43
44
    /**
45
     * @param string $sSize
46
     *
47
     * @return int
48
     */
49
    public static function convertPHPSizeToBytes($sSize)
50
    {
51
        $sSize = trim($sSize);
52
53
        if (is_numeric($sSize)) {
54
            return (int) $sSize;
55
        }
56
57
        $sSuffix = substr($sSize, -1);
58
        $iValue  = substr($sSize, 0, -1);
59
60
        //missing breaks are important
61
        switch (strtoupper($sSuffix)) {
62
            case 'P':
63
                $iValue *= 1024;
64
                // no break
65
            case 'T':
66
                $iValue *= 1024;
67
                // no break
68
            case 'G':
69
                $iValue *= 1024;
70
                // no break
71
            case 'M':
72
                $iValue *= 1024;
73
                // no break
74
            case 'K':
75
                $iValue *= 1024;
76
                break;
77
        }
78
79
        return $iValue;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $iValue returns the type string which is incompatible with the documented return type integer.
Loading history...
80
    }
81
}
82