Gdn_Upload::unformatFileSize PHP Method

unformatFileSize() public static method

Take a string formatted filesize and return the number of bytes.
public static unformatFileSize ( string $Formatted ) : integer
$Formatted string The formatted filesize.
return integer The number of bytes in the string.
    public static function unformatFileSize($Formatted)
    {
        $Units = array('B' => 1, 'K' => 1024, 'M' => 1024 * 1024, 'G' => 1024 * 1024 * 1024, 'T' => 1024 * 1024 * 1024 * 1024);
        if (preg_match('/([0-9.]+)\\s*([A-Z]*)/i', $Formatted, $Matches)) {
            $Number = floatval($Matches[1]);
            $Unit = strtoupper(substr($Matches[2], 0, 1));
            $Mult = val($Unit, $Units, 1);
            $Result = round($Number * $Mult, 0);
            return $Result;
        } else {
            return false;
        }
    }

Usage Example

 /**
  * Placed these components everywhere due to some Web sites loading the
  * editor in some areas where the values were not yet injected into HTML.
  */
 public function base_render_before(&$Sender)
 {
     // Don't render any assets for editor if it's embedded. This effectively
     // disables the editor from embedded comments. Some HTML is still
     // inserted, because of the BeforeBodyBox handler, which does not contain any data relating to embedded content.
     if ($this->isEmbeddedComment($Sender)) {
         return false;
     }
     $c = Gdn::controller();
     // If user wants to modify styling of Wysiwyg content in editor,
     // they can override the styles with this file.
     $CssInfo = AssetModel::cssPath('wysiwyg.css', 'plugins/editor');
     if ($CssInfo) {
         $CssPath = asset($CssInfo[1]);
     }
     // Load JavaScript used by every editor view.
     $c->addJsFile('editor.js', 'plugins/editor');
     // Fileuploads
     $c->addJsFile('jquery.ui.widget.js', 'plugins/editor');
     $c->addJsFile('jquery.iframe-transport.js', 'plugins/editor');
     $c->addJsFile('jquery.fileupload.js', 'plugins/editor');
     // Set definitions for JavaScript to read
     $c->addDefinition('editorVersion', $this->pluginInfo['Version']);
     $c->addDefinition('editorInputFormat', $this->Format);
     $c->addDefinition('editorPluginAssets', $this->AssetPath);
     $c->addDefinition('wysiwygHelpText', t('editor.WysiwygHelpText', 'You are using <a href="https://en.wikipedia.org/wiki/WYSIWYG" target="_new">WYSIWYG</a> in your post.'));
     $c->addDefinition('bbcodeHelpText', t('editor.BBCodeHelpText', 'You can use <a href="http://en.wikipedia.org/wiki/BBCode" target="_new">BBCode</a> in your post.'));
     $c->addDefinition('htmlHelpText', t('editor.HtmlHelpText', 'You can use <a href="http://htmlguide.drgrog.com/cheatsheet.php" target="_new">Simple HTML</a> in your post.'));
     $c->addDefinition('markdownHelpText', t('editor.MarkdownHelpText', 'You can use <a href="http://en.wikipedia.org/wiki/Markdown" target="_new">Markdown</a> in your post.'));
     $c->addDefinition('textHelpText', t('editor.TextHelpText', 'You are using plain text in your post.'));
     $c->addDefinition('editorWysiwygCSS', $CssPath);
     $additionalDefinitions = array();
     $this->EventArguments['definitions'] =& $additionalDefinitions;
     $this->fireEvent('GetJSDefinitions');
     // Make sure we still have an array after all event handlers have had their turn and iterate through the result.
     if (is_array($additionalDefinitions)) {
         foreach ($additionalDefinitions as $defKey => $defVal) {
             $c->addDefinition($defKey, $defVal);
         }
         unset($defKey, $defVal);
     }
     // Set variables for file uploads
     $PostMaxSize = Gdn_Upload::unformatFileSize(ini_get('post_max_size'));
     $FileMaxSize = Gdn_Upload::unformatFileSize(ini_get('upload_max_filesize'));
     $ConfigMaxSize = Gdn_Upload::unformatFileSize(c('Garden.Upload.MaxFileSize', '1MB'));
     $MaxSize = min($PostMaxSize, $FileMaxSize, $ConfigMaxSize);
     $c->addDefinition('maxUploadSize', $MaxSize);
     // Set file input name
     $c->addDefinition('editorFileInputName', $this->editorFileInputName);
     $Sender->setData('_editorFileInputName', $this->editorFileInputName);
     // Save allowed file types
     $c->addDefinition('allowedFileExtensions', json_encode(c('Garden.Upload.AllowedFileExtensions')));
     // Get max file uploads, to be used for max drops at once.
     $c->addDefinition('maxFileUploads', ini_get('max_file_uploads'));
     // Set canUpload definition here, but not Data (set in BeforeBodyBox) because it overwrites.
     $c->addDefinition('canUpload', $this->canUpload);
 }
All Usage Examples Of Gdn_Upload::unformatFileSize