#10717: Introduced a new function to t3lib_div::getMaxUploadFileSize() to have a...
authorBenni Mack <benni.mack@typo3.org>
Tue, 31 Mar 2009 13:40:53 +0000 (13:40 +0000)
committerBenni Mack <benni.mack@typo3.org>
Tue, 31 Mar 2009 13:40:53 +0000 (13:40 +0000)
 function to detect the maximum upload limit, was: No simple way to get the maximum
 upload size in TYPO3

git-svn-id: https://svn.typo3.org/TYPO3v4/Core/trunk@5249 709f56b5-9817-0410-a4d7-c38de5d9e867

ChangeLog
t3lib/class.t3lib_div.php
t3lib/class.t3lib_extfilefunc.php
tests/t3lib/t3lib_div_testcase.php

index 2dc7331..e1526f3 100755 (executable)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,12 @@
+2009-03-31  Benjamin Mack  <benni@typo3.org>
+
+       * #10717: Introduced a new function to t3lib_div::getMaxUploadFileSize() to have a central
+         function to detect the maximum upload limit, was: No simple way to get the maximum 
+         upload size in TYPO3
+
 2009-03-31  Oliver Hader  <oliver@typo3.org>
 
-       * Flollow-up to bug #10025: Enabled to cut off image profiles for image resizing by default
+       * Follow-up to bug #10025: Enabled to cut off image profiles for image resizing by default
        * Fixed bug #10190: Call to undefined method t3lib_div::calluserfunc() in class.tslib_fe.php
        * Fixed bug #10431: Call to undefined method SC_wizard_list::printContent()
 
index 688de2e..b4aec4f 100644 (file)
@@ -3124,9 +3124,52 @@ final class t3lib_div {
                return $path;
        }
 
+       /**
+        * Returns the maximum upload size for a file that is allowed. Measured in KB.
+        * This might be handy to find out the real upload limit that is possible for this 
+        * TYPO3 installation. The first parameter can be used to set something that overrides
+        * the maxFileSize, usually for the TCA values.
+        *
+        * @param       integer         $localLimit: the number of Kilobytes (!) that should be used as
+        *                                              the initial Limit, otherwise $TYPO3_CONF_VARS['BE']['maxFileSize'] will be used
+        * @return      integer         the maximum size of uploads that are allowed (measuered in kilobytes)
+        */
+       public static function getMaxUploadFileSize($localLimit = 0) {
+                       // don't allow more than the global max file size at all
+               $t3Limit = (intval($localLimit > 0 ? $localLimit : $GLOBALS['TYPO3_CONF_VARS']['BE']['maxFileSize']));
+                       // as TYPO3 is handling the file size in KB, multiply by 1024 to get bytes
+               $t3Limit = $t3Limit * 1024;
 
+                       // check for PHP restrictions of the maximum size of one of the $_FILES
+               $phpUploadLimit = self::getBytesFromSizeMeasurement(ini_get('upload_max_filesize'));
+                       // check for PHP restrictions of the maximum $_POST size
+               $phpPostLimit = self::getBytesFromSizeMeasurement(ini_get('post_max_size'));
+                       // if the total amount of post data is smaller (!) than the upload_max_filesize directive,
+                       // then this is the real limit in PHP
+               $phpUploadLimit = ($phpPostLimit < $phpUploadLimit ? $phpPostLimit : $phpUploadLimit);
 
+                       // is the allowed PHP limit (upload_max_filesize) lower than the TYPO3 limit?, also: revert back to KB
+               return floor($phpUploadLimit < $t3Limit ? $phpUploadLimit : $t3Limit) / 1024;
+       }
 
+       /**
+        * Gets the bytes value from a measurement string like "100k".
+        *
+        * @param       string          $measurement: The measurement (e.g. "100k")
+        * @return      integer         The bytes value (e.g. 102400)
+        */
+       public static function getBytesFromSizeMeasurement($measurement) {
+               if (stripos($measurement, 'G')) {
+                       $bytes = intval($measurement) * 1024 * 1024 * 1024;
+               } else if (stripos($measurement, 'M')) {
+                       $bytes = intval($measurement) * 1024 * 1024;
+               } else if (stripos($measurement, 'K')) {
+                       $bytes = intval($measurement) * 1024;
+               } else {
+                       $bytes = intval($measurement);
+               }
+               return $bytes;
+       }
 
 
 
index 08de1c2..d2da491 100644 (file)
@@ -163,11 +163,11 @@ class t3lib_extFileFunctions extends t3lib_basicFileFunctions     {
                $this->unzipPath = $GLOBALS['TYPO3_CONF_VARS']['BE']['unzip_path'];
 
                $maxFileSize = intval($GLOBALS['TYPO3_CONF_VARS']['BE']['maxFileSize']);
-               if ($maxFileSize>0)     {
+               if ($maxFileSize > 0) {
                        $this->maxCopyFileSize = $maxFileSize;
                        $this->maxMoveFileSize = $maxFileSize;
-                       $this->maxUploadFileSize = $maxFileSize;
                }
+               $this->maxUploadFileSize = t3lib_div::getMaxUploadFileSize();
 
                        // Initializing file processing commands:
                $this->fileCmdMap = $fileCmds;
index 74dc675..da57fac 100644 (file)
@@ -97,6 +97,29 @@ class t3lib_div_testcase extends tx_phpunit_testcase {
 
                $this->assertEquals($expectedArray, $actualArray);
        }
+
+       /**
+        * Checks whether measurement strings like "100k" return the accordant
+        * byte representation like 102400 in this case.
+        *
+        * @test
+        */
+       public function checkGetBytesFromSizeMeasurement() {
+               $this->assertEquals(
+                       '102400',
+                       t3lib_div::getBytesFromSizeMeasurement('100k')
+               );
+
+               $this->assertEquals(
+                       '104857600',
+                       t3lib_div::getBytesFromSizeMeasurement('100m')
+               );
+
+               $this->assertEquals(
+                       '107374182400',
+                       t3lib_div::getBytesFromSizeMeasurement('100g')
+               );
+       }
 }
 
 ?>
\ No newline at end of file