diff options
author | vincent-peugnet <v.peugnet@free.fr> | 2020-05-18 17:08:11 +0200 |
---|---|---|
committer | vincent-peugnet <v.peugnet@free.fr> | 2020-05-18 17:08:50 +0200 |
commit | 121af5f84ad9262e5c509dc1dd741166ca1dc3de (patch) | |
tree | 3b75d4870894045d2d28b156db8637112c42b337 /app/fn | |
parent | ceff73a9a7ab210034e5d8b0452d18135ce7137d (diff) | |
download | wcms-121af5f84ad9262e5c509dc1dd741166ca1dc3de.tar.gz wcms-121af5f84ad9262e5c509dc1dd741166ca1dc3de.zip |
file :show max upload size
fix #101
Diffstat (limited to 'app/fn')
-rw-r--r-- | app/fn/fn.php | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/app/fn/fn.php b/app/fn/fn.php index 103062a..e0e5f47 100644 --- a/app/fn/fn.php +++ b/app/fn/fn.php @@ -433,3 +433,39 @@ function accessfile(string $path, bool $createdir = false): bool return false; } } + +// Returns a file size limit in bytes based on the PHP upload_max_filesize +// and post_max_size +function file_upload_max_size() +{ + static $max_size = -1; + + if ($max_size < 0) { + // Start with post_max_size. + $post_max_size = parse_size(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_size(ini_get('upload_max_filesize')); + if ($upload_max > 0 && $upload_max < $max_size) { + $max_size = $upload_max; + } + } + return $max_size; +} + +function parse_size($size) +{ + $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size. + $size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size. + $size = floatval($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); + } +} |