Human readable filesize()

@author rommel at rommelsantor dot com
@link http://php.net/manual/en/function.filesize.php#106569

@param string  $filename Path to the file
@param integer $decimals Amount of decimals

@return string
438399
public static function filesize($filename, $decimals = 2)
    {
        $bytes = filesize($filename);
        $factor = floor((strlen($bytes) - 1) / 3);
        $size = 'BKMGTP';
        return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
    }
306969
public static function getFileExtension($filename)
    {
        $filename = basename($filename);

        // remove possible query string
        $pos = strpos($filename, '?');
        if ($pos !== false) {
            $filename = substr($filename, 0, $pos);
        }

        // retrieve the extension
        $pos = strrpos($filename, '.');
        if ($pos !== false) {
            return strtolower(substr($filename, $pos + 1));
        }

        return '';
    }
1238440
protected static function getFilesize($filesize)
    {
        $value = (float)($filesize);
        $unit = substr($filesize, -1);

        switch ($unit) {
            case 'K':
                return $value / 1024;
            case 'G':
                return $value * 1024;
            case 'M':
            default:
                return $value;
        }
    }
806656
public function parseFileSize($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.
        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);
        }
    }
767430
protected function checkFileSize($filename) {

        if ( filesize($filename) > $this->config['max_size'] ) {
            $this->errNum = 1;
        }

        //如果是图片还要检查图片的宽度和高度是否超标
        $size = getimagesize($filename);
        if ( $size != false ) {

            $this->fileInfo['is_image'] = 1;
            if ( ($this->config['max_width'] > 0 && $size[0] > $this->config['max_width'])
                || ($this->config['max_height'] > 0 && $size[1] > $this->config['max_height']) )  {
                $this->errNum = 9;
            }
            $this->fileInfo['image_width'] = $size[0];
            $this->fileInfo['image_height'] = $size[1];

        } else {

            $this->fileInfo['is_image'] = 0;

        }

	}
1140061
public static function ImageSize($Path, $Filename = FALSE) {
      if (!$Filename)
         $Filename = $Path;
      
      if (in_array(strtolower(pathinfo($Filename, PATHINFO_EXTENSION)), array('gif', 'jpg', 'jpeg', 'png'))) {
         $ImageSize = @getimagesize($Path);
         if (!is_array($ImageSize) || !in_array($ImageSize[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)))
            return array(0, 0, FALSE);
         return $ImageSize;
      }
      return array(0, 0, FALSE);
   }
585116
public function size(String $fileName) : stdClass
    {
        if( $this->mime->type($fileName, 0) === 'image' )
        {
            $data = getimagesize($fileName);
        }
        elseif( is_string($fileName) )
        {
            $data = getimagesizefromstring($fileName);
        }
        else
        {
            throw new InvalidArgumentException(NULL, '[file]');
        }

        return (object)
        [
            'width'     => $data[0],
            'height'    => $data[1],
            'extension' => $this->extension($data[2]),
            'img'       => $data[3],
            'bits'      => $data['bits'],
            'mime'      => $data['mime']
        ];
    }
1330582
private function size($filename)
    {
        if ($this->isSFTP()) {
            $size = (int)@$this->ftp->size($filename);
        } else {
            $size = @ftp_size($this->ftp, $filename);
        }
        if ($size > 0) {
            return $size;
        }
        $this->fail("couldn't get the file size for {$filename}");
    }
1201132
private function modified($filename)
    {
        if ($this->isSFTP()) {
            $info = @$this->ftp->lstat($filename);
            if ($info) {
                return $info['mtime'];
            }
        } else {
            if ($time = @ftp_mdtm($this->ftp, $filename)) {
                return $time;
            }
        }
        $this->fail("couldn't get the file size for {$filename}");
    }
1145744
public static function formatSize($size, $unit = null, $decimals = 2) {


        // Initialize the units.
        $units = static::getUnits();

        // Find the unit.
        $index = array_search($unit, $units);
        if (null !== $unit && false === $index) {
            throw new IllegalArgumentException("The unit \"" . $unit . "\" does not exists");
        }

        // Initialize the output.
        $output = $size;

        $iteration = 0;
        while (self::FILE_SIZE_DIVIDER <= $output || $iteration < $index) {
            $output /= self::FILE_SIZE_DIVIDER;
            ++$iteration;
        }

        // Return the output.
        return implode(" ", [sprintf("%." . $decimals . "f", $output), $units[$iteration]]);
    }
1192251
public static function formatSize($size, $decimalPrecision = 2) {

        if ($size < 1024)                           return $size . ' B';
        elseif ($size < 1048576)                   return round($size / 1024, $decimalPrecision) . ' kB';
        elseif ($size < 1073741824)                return round($size / 1048576, $decimalPrecision) . ' MB';
        elseif ($size < 1099511627776)             return round($size / 1073741824, $decimalPrecision) . ' GB';
        elseif ($size < 1125899906842624)          return round($size / 1099511627776, $decimalPrecision) . ' TB';
        elseif ($size < 1152921504606846976)       return round($size / 1125899906842624, $decimalPrecision) . ' PB';
        else return round($size / 1152921504606846976, $decimalPrecision) . ' EB';
    }
154309
public function human_filesize($bytes, $decimals = 0)
    {
        $size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
        $factor = floor((strlen($bytes) - 1) / 3);

        return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)).@$size[$factor];
    }
1299792
private function guessFromExtension($filename)
    {
        $output = 'text';
        $pInfo = pathinfo($filename);
        if (isset($pInfo['extension'])) {
            foreach ($this->fileTypes as $extension => $lexer) {
                if ($extension === $pInfo['extension']) {
                    return $lexer;
                }
            }
        }

        return $output;
    }
294576
public static function getFileExtAttr($filename) {
        if (file_exists($filename)) {
            $fp = fileperms($filename) << 16;
            return $fp | (is_dir($filename) ? self::S_DOS_D : self::S_DOS_A);
        }
        return FALSE;
    }
1241295
public static function dirSize($dirName)
    {
        $totalSize = 0;
        if ($dirStream = opendir($dirName)) {
            while (false !== ($fileName = readdir($dirStream))) {
                if ($fileName !== '.' && $fileName !== '..') {
                    if (is_file($dirName.'/'.$fileName)) {
                        $totalSize += filesize($dirName.'/'.$fileName);
                    }

                    if (is_dir($dirName.'/'.$fileName)) {
                        $totalSize += self::dirSize($dirName.'/'.$fileName);
                    }
                }
            }
        }

        closedir($dirStream);

        return $totalSize;
    }
831063
public function filesize()
	{
		if (!$this->filename)
		{
			throw new \RuntimeException('File not open');
		}

		// Capture PHP errors
		$php_errormsg = '';
		$track_errors = ini_get('track_errors');
		ini_set('track_errors', true);
		$res = @filesize($this->filename);

		if (!$res)
		{
			$tmp_error = '';

			if ($php_errormsg)
			{
				// Something went wrong.
				// Store the error in case we need it.
				$tmp_error = $php_errormsg;
			}

			$res = Helper::remotefsize($this->filename);

			if (!$res)
			{
				if ($tmp_error)
				{
					// Use the php_errormsg from before
					throw new \RuntimeException($tmp_error);
				}
				else
				{
					// Error but nothing from php? How strange! Create our own
					throw new \RuntimeException('Failed to get file size. This may not work for all streams.');
				}
			}
			else
			{
				$this->filesize = $res;
				$retval = $res;
			}
		}
		else
		{
			$this->filesize = $res;
			$retval = $res;
		}

		// Restore error tracking to what it was before.
		ini_set('track_errors', $track_errors);

		// Return the result
		return $retval;
	}
81928
public static function getSize($filename) {
        if (false === file_exists($filename)) {
            throw new FileNotFoundException($filename);
        }
        clearstatcache();
        return filesize($filename);
    }