Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
fraz671r committed Aug 7, 2023
2 parents ed95409 + 8ac8d01 commit 915837f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
61 changes: 61 additions & 0 deletions src/PhPease/Php/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types=1);

/*
* This file is part of the PhPease package.
*
* (c) Timo Reith <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PhPease\Php;

use function PhPease\Convert\bytes_to_human_readable;

/**
* @return float|int
*/
function get_file_upload_max_size() {

static $max_size = -1;

if ($max_size < 0) {
// Start with post_max_size.
$post_max_size = parse_ini_filesize(ini_get('post_max_size'));

if ($post_max_size > 0) {
$max_size = $post_max_size;
}

// If upload_max_size is less, then reduce. Except if upload_max_size is
// zero, which indicates no limit.
$upload_max = parse_ini_filesize(ini_get('upload_max_filesize'));

if ($upload_max > 0 && $upload_max < $max_size) {
$max_size = $upload_max;
}
}

return $max_size;
}

function get_file_upload_max_size_human_readable(): string
{
return bytes_to_human_readable(get_file_upload_max_size());
}

function parse_ini_filesize($size): float
{
// Remove the non-unit characters from the size.
$unit = preg_replace('/[^bkmgtpezy]/i', '', $size);
// Remove the non-numeric characters from the size.
$size = (float)preg_replace('/[^0-9\.]/', '', $size);

if ($unit) {
// Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
return round($size * pow(1024, stripos('bkmgtpezy', $unit[0])));
} else {
return round($size);
}
}
3 changes: 2 additions & 1 deletion src/PhPease/functions_include.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
require_once __DIR__ . '/Convert/functions.php';
require_once __DIR__ . '/Request/functions.php';
require_once __DIR__ . '/String/functions.php';
require_once __DIR__ . '/Variable/functions.php';
require_once __DIR__ . '/Variable/functions.php';
require_once __DIR__ . '/Php/functions.php';
15 changes: 15 additions & 0 deletions tests/PhpFunctionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use function PhPease\Php\parse_ini_filesize;

final class PhpFunctionsTest extends TestCase
{
public function testParseIniFilesize()
{
$this->assertEquals(2097152, parse_ini_filesize('2M'));
$this->assertEquals(4194304, parse_ini_filesize('4M'));
$this->assertEquals(536870912, parse_ini_filesize('512 M'));
$this->assertEquals(2147483648, parse_ini_filesize('2G'));
$this->assertEquals(400, parse_ini_filesize('400'));
}
}

0 comments on commit 915837f

Please sign in to comment.