From 0a2c3218acf2baa344d3ffd1c88c74cc1cca2740 Mon Sep 17 00:00:00 2001 From: kink Date: Tue, 11 Mar 2003 15:05:11 +0000 Subject: [PATCH] Calc the max size for an uploaded file and display it to the user. This is informational to prevent large file uploads to fail. Becasue PHP stores sizes like "8M" we have to calculate the bytesize first. git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@4640 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- src/compose.php | 67 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 11 deletions(-) diff --git a/src/compose.php b/src/compose.php index 22a70278..c946388e 100644 --- a/src/compose.php +++ b/src/compose.php @@ -1032,23 +1032,44 @@ function showInputForm ($session, $values=false) { /* This code is for attachments */ if ((bool) ini_get('file_uploads')) { - echo ' ' . "\n" . - ' ' . "\n" . + + /* Calculate the max size for an uploaded file. + * This is advisory for the user because we can't actually prevent + * people to upload too large files. */ + $sizes = array(); + /* php.ini vars which influence the max for uploads */ + $configvars = array('post_max_size', 'memory_limit', 'upload_max_filesize'); + foreach($configvars as $var) { + /* skip 0 or empty values */ + if( $size = getByteSize(ini_get($var)) ) { + $sizes[] = $size; + } + } + + if(count($sizes) > 0) { + $maxsize = '(max. ' . show_readable_size( min( $sizes ) ) . ')'; + } else { + $maxsize = ''; + } + + echo ' ' . "\n" . + ' ' . "\n" . ' ' . "\n" . - ' ' . "\n" . - ' ' . "\n" . + '
' . "\n" . + '
' . "\n" . ' ' . "\n" . - ' ' . "\n" . - html_tag( 'td', '', 'right', '', 'VALIGN=MIDDLE' ) . - _("Attach:") . '' . "\n" . - html_tag( 'td', '', 'left', '', 'VALIGN=MIDDLE' ) . - ' ' . "\n" . + ' ' . "\n" . + html_tag( 'td', '', 'right', '', 'valign="middle"' ) . + _("Attach:") . '' . "\n" . + html_tag( 'td', '', 'left', '', 'valign="middle"' ) . + ' ' . "\n" . '   ' . "\n" . - ' ' . "\n" . - ' ' . "\n"; + $maxsize . + ' ' . "\n" . + ' ' . "\n"; $s_a = array(); @@ -1238,7 +1259,31 @@ function ClearAttachments($composeMessage) { } } +/* parse values like 8M and 2k into bytes */ +function getByteSize($ini_size) { + + if(!$ini_size) return FALSE; + $ini_size = trim($ini_size); + + switch(strtoupper(substr($ini_size, -1))) { + case 'G': + $bytesize = 1073741824; + break; + case 'M': + $bytesize = 1048576; + break; + case 'K': + $bytesize = 1024; + break; + default: + $bytesize = 1; + } + + $bytesize *= (int)substr($ini_size, 0, -1); + + return $bytesize; +} /* temporary function to make use of the deliver class. -- 2.25.1