File size: 1,814 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php

namespace Mautic\CoreBundle\Helper;

class FileHelper
{
    public const BYTES_TO_MEGABYTES_RATIO = 1_048_576;

    public static function convertBytesToMegabytes($b): float
    {
        return round($b / self::BYTES_TO_MEGABYTES_RATIO, 2);
    }

    public static function convertMegabytesToBytes($mb)
    {
        return $mb * self::BYTES_TO_MEGABYTES_RATIO;
    }

    public static function getMaxUploadSizeInBytes(): int
    {
        $maxPostSize   = self::convertPHPSizeToBytes(ini_get('post_max_size'));
        $maxUploadSize = self::convertPHPSizeToBytes(ini_get('upload_max_filesize'));
        $memoryLimit   = self::convertPHPSizeToBytes(ini_get('memory_limit'));

        return min($maxPostSize, $maxUploadSize, $memoryLimit);
    }

    public static function getMaxUploadSizeInMegabytes(): float
    {
        $maxUploadSizeInBytes = self::getMaxUploadSizeInBytes();

        return self::convertBytesToMegabytes($maxUploadSizeInBytes);
    }

    /**
     * @param string $sSize
     */
    public static function convertPHPSizeToBytes($sSize): int
    {
        $sSize = trim($sSize);

        if (is_numeric($sSize)) {
            return (int) $sSize;
        }

        $sSuffix = substr($sSize, -1);
        $iValue  = (int) substr($sSize, 0, -1);

        // missing breaks are important
        switch (strtoupper($sSuffix)) {
            case 'P':
                $iValue *= 1024;
                // no break
            case 'T':
                $iValue *= 1024;
                // no break
            case 'G':
                $iValue *= 1024;
                // no break
            case 'M':
                $iValue *= 1024;
                // no break
            case 'K':
                $iValue *= 1024;
                break;
        }

        return $iValue;
    }
}