0011296: can't upload files when upload_max_filesize is given in GB
authorCornelius Weiß <c.weiss@metaways.de>
Wed, 2 Sep 2015 17:18:05 +0000 (19:18 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Tue, 8 Sep 2015 12:53:10 +0000 (14:53 +0200)
* cope with known prefixes
* optimize code

https://forge.tine20.org/view.php?id=11296

Change-Id: I3886095869f69952ad852cc651d562099d06c769
Reviewed-on: http://gerrit.tine20.com/customers/2180
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
Tested-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Tinebase/HelperTests.php
tine20/Tinebase/Helper.php

index d89c5a2..ccb9f39 100644 (file)
@@ -83,6 +83,7 @@ class Tinebase_HelperTests extends PHPUnit_Framework_TestCase
         $this->assertEquals(1024, Tinebase_Helper::convertToBytes('1024'));
         $this->assertEquals(1024, Tinebase_Helper::convertToBytes('1K'));
         $this->assertEquals(1024*1024, Tinebase_Helper::convertToBytes('1M'));
+        $this->assertEquals(1024*1024*1024, Tinebase_Helper::convertToBytes('1G'));
         
     }
 
index 58001d0..7fbca3c 100644 (file)
@@ -48,7 +48,7 @@ class Tinebase_Helper
         
         return hash_final($ctx);
     }
-    
+
     /**
      * converts string with M or K to bytes integer
      * - for example: 50M -> 52428800
@@ -56,28 +56,17 @@ class Tinebase_Helper
      * @param mixed $_value
      * @return integer
      */
-    public static function convertToBytes($_value)
+    public static function convertToBytes($value)
     {
-        if (is_int($_value) || ! $_value) {
-            $bytes = $_value;
-        } else {
-            if (preg_match("/M/", $_value)) {
-                $value = substr($_value, 0, strpos($_value, 'M'));
-                $factor = 1024 * 1024;
-            } elseif (preg_match("/K/", $_value)) {
-                $value = substr($_value, 0, strpos($_value, 'K'));
-                $factor = 1024;
-            } elseif (is_string($_value)) {
-                $value = $_value;
-                $factor = 1;
-            } else {
-                throw new Exception('Argument type not supported:' . gettype($_value));
-            }
-            
-            $bytes = intval($value) * $factor;
+        $powers = 'KMGTPEZY';
+        $power = 0;
+
+        if (preg_match('/(\d+)\s*([' . $powers . '])/', $value, $matches)) {
+            $value = $matches[1];
+            $power = strpos($powers, $matches[2]) + 1;
         }
-        
-        return $bytes;
+
+        return (int)$value * pow(1024, $power);
     }
     
     /**