Set Max Filesize

@access public
@param  $bytes
176273
public function setMaxFilesize($bytes)
    {
        // Make compatible with PHP version both before and after 5.5.0. PHP 5.5.0 added the cURL resource as the first
        // argument to the CURLOPT_PROGRESSFUNCTION callback.
        $gte_v550 = version_compare(PHP_VERSION, '5.5.0') >= 0;
        if ($gte_v550) {
            $callback = function ($resource, $download_size, $downloaded, $upload_size, $uploaded) use ($bytes) {
                // Abort the transfer when $downloaded bytes exceeds maximum $bytes by returning a non-zero value.
                return $downloaded > $bytes ? 1 : 0;
            };
        } else {
            $callback = function ($download_size, $downloaded, $upload_size, $uploaded) use ($bytes) {
                return $downloaded > $bytes ? 1 : 0;
            };
        }

        $this->progress($callback);
    }
735157
public static function Bytes($Bytes, $Precision = 2) {
      $Units = array('B', 'K', 'M', 'G', 'T');
      $Bytes = max($Bytes, 0);
      $Pow = floor(($Bytes ? log($Bytes) : 0) / log(1024));
      $Pow = min($Pow, count($Units) - 1);
      $Bytes /= pow(1024, $Pow);
      return round($Bytes, $Precision) . $Units[$Pow]; 
   }
1173580
public function setMaxWidth($maxWidth)
    {
        $maxWidth = (int) $maxWidth;
        $this->maxWidth = $maxWidth > 0 ? $maxWidth : null;
    }
268393
public function formatFilesize($bytes)
    {
        $units = array('bytes', 'kb', 'MB', 'GB', 'TB', 'PB');
        $level = floor(log($bytes)/log(1024));

        $output = sprintf('%.2f %s', ($bytes/pow(1024, floor($level))), $units[$level]);

        return $output;
    }
94301
public function setChunkSize($chunkSize = 4194304)
    {
        if (!is_int($chunkSize)) {
            throw new Exception('Expecting chunk size to be an integer, got ' . gettype($chunkSize));
        } elseif ($chunkSize > 157286400) {
            throw new Exception('Chunk size must not exceed 157286400 bytes, got ' . $chunkSize);
        } else {
            $this->chunkSize = $chunkSize;
        }
    }
283244
public function shift($bytes)
    {
        $bufferLen = strlen($this->buffer);

        if ($bytes === $bufferLen) {
            $return       = $this->buffer;
            $this->buffer = '';
        } else {
            $return       = substr($this->buffer, 0, $bytes);
            $this->buffer = substr($this->buffer, $bytes);
        }

        return $return;
    }
1099994
private function fileSizeConvert(int $bytes)
    {
        $bytes = floatval($bytes);
        $result = "0 B";
        $arBytes = array(
            0 => array(
                "UNIT" => "TB",
                "VALUE" => pow(1024, 4)
            ),
            1 => array(
                "UNIT" => "GB",
                "VALUE" => pow(1024, 3)
            ),
            2 => array(
                "UNIT" => "MB",
                "VALUE" => pow(1024, 2)
            ),
            3 => array(
                "UNIT" => "KB",
                "VALUE" => 1024
            ),
            4 => array(
                "UNIT" => "B",
                "VALUE" => 1
            ),
        );

        foreach ($arBytes as $arItem) {
            if ($bytes >= $arItem["VALUE"]) {
                $result = $bytes / $arItem["VALUE"];
                $result = str_replace(".", ",", strval(round($result, 2)))." ".$arItem["UNIT"];
                break;
            }
        }
        return $result;
    }
100955
protected function formatBytes(int $bytes)
    {
        if (!$this->useFormatting) {
            return $bytes;
        }

        if ($bytes > 1024 * 1024) {
            return round($bytes / 1024 / 1024, 2).' MB';
        } elseif ($bytes > 1024) {
            return round($bytes / 1024, 2).' KB';
        }

        return $bytes . ' B';
    }
1200140
static public function bytesToSize($bytes, $precision = 2)
    {
        $kilobyte = 1024;
        $megabyte = $kilobyte * 1024;
        $gigabyte = $megabyte * 1024;
        $terabyte = $gigabyte * 1024;

        if (($bytes >= 0) && ($bytes < $kilobyte)) {
            return $bytes . ' B';

        } elseif (($bytes >= $kilobyte) && ($bytes < $megabyte)) {
            return round($bytes / $kilobyte, $precision) . ' KB';

        } elseif (($bytes >= $megabyte) && ($bytes < $gigabyte)) {
            return round($bytes / $megabyte, $precision) . ' MB';

        } elseif (($bytes >= $gigabyte) && ($bytes < $terabyte)) {
            return round($bytes / $gigabyte, $precision) . ' GB';

        } elseif ($bytes >= $terabyte) {
            return round($bytes / $terabyte, $precision) . ' TB';
        } else {
            return $bytes . ' B';
        }
    }
120148
public function charsUntil($bytes, $max = null) {
        if ($this->char < $this->EOF) {
            if ($max === 0 || $max) {
                $len = strcspn($this->data, $bytes, $this->char, $max);
            } else {
                $len = strcspn($this->data, $bytes, $this->char);
            }
            $string = (string) substr($this->data, $this->char, $len);
            $this->char += $len;
            return $string;
        } else {
            return false;
        }
    }
1165929
public function setMaxFileSize($fileSize, $unit = 'M')
    {
        switch ($unit) {
            case 'B':
                $fileSize = (int)$fileSize;
                break;
            case 'K':
                $fileSize = (int)$fileSize * 1000;
                break;
            case 'M':
                $fileSize = (int)$fileSize * 1000000;
                break;
            case 'G':
                $fileSize = (int)$fileSize * 1000000000;
                break;
        }

        $this->allowedFileSize[ 'max' ] = (int)$fileSize;

        return $this;
    }
1042590
private function limit_filesize($length) {

        // Limit to $this->filesizelimit.
        if (is_numeric($this->filesizelimit) && $length > $this->filesizelimit) {
            $length = floor($this->filesizelimit);
        }

        return $length;
    }
1286471
final public function randomNumber($bytes = 32)
    {
        $random_data = openssl_random_pseudo_bytes($bytes, $cstrong);

        if (!$cstrong || !$random_data) {
            return false;
        } else {
            return bin2hex($random_data);
        }
    }
156528
static function maxUploadFileSize() {
		$file_max = Strings::parsePhpNumber(ini_get("post_max_size"));
		$post_max = Strings::parsePhpNumber(ini_get("upload_max_filesize"));
		$php_max = min($file_max,$post_max);
		return $php_max;
	}
231957
public static function resizeBytes($bytes)
    {
        static $units = ['b', 'kb', 'mb', 'gb', 'tb', 'pb'];

        /** @noinspection PhpIllegalArrayKeyTypeInspection */

        return @round($bytes / pow(1024, ($i = floor(log($bytes, 1024)))), 2) . $units[$i];
    }
1250375
public static function maxUpload ($default = null)
		{
			$bytes = self::maxBytesUpload();

			return ( $bytes == null ) ? $default : self::size($bytes);
		}
1138892
public function maxFileSize($size, $unit = "B")
	{
		$unit = strtoupper($unit);
		if(! is_numeric($size))
		{
			throw new Exception("Invalid file size: expects integer");
		}
		if($unit === "B" || preg_match("/^BYTE(S)?$/", $unit))
		{
			$this->maxFileSize = $size;
		}
		elseif($unit === "KB" || preg_match("/^KILOBYTE(S)?$/", $unit))
		{
			$this->maxFileSize = ($size * 1000);
		}
		elseif($unit === "MB" || preg_match("/^MEGABYTE(S)?$/", $unit))
		{
			$this->maxFileSize = ($size * 1000000);
		}
		else
		{
			throw new Exception("Invalid unit in setMaxFileSize: Expects 'B', 'KB' or 'MB'.");
		}
		if($this->maxFileSize > UploadedFile::getMaxFilesize())
		{
			throw new Exception("Max file size cannot exceed upload_max_filesize in php.ini");
		}

		return $this;
	}