N98\Util\Filesystem::humanFileSize PHP Method

humanFileSize() public static method

See also: http://www.php.net/manual/en/function.filesize.php#106569
public static humanFileSize ( integer $bytes, integer $decimals = 2 ) : string
$bytes integer
$decimals integer
return string
    public static function humanFileSize($bytes, $decimals = 2)
    {
        $units = array('B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
        $factor = floor((strlen($bytes) - 1) / 3);
        return sprintf("%.{$decimals}f%s", $bytes / pow(1024, $factor), $units[$factor]);
    }

Usage Example

Example #1
0
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  *
  * @throws RuntimeException
  * @return void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->detectMagento($output);
     if ($this->initMagento()) {
         $fileName = $input->getArgument('log_filename');
         if ($fileName === null) {
             $path = $this->askLogFile($output);
         } else {
             $path = $this->getLogDir() . DIRECTORY_SEPARATOR . $fileName;
         }
         if ($this->logfileExists(basename($path))) {
             $size = @filesize($path);
             if ($size === false) {
                 throw new RuntimeException('Couldn\\t detect filesize.');
             }
         } else {
             $size = 0;
         }
         if ($input->getOption('human')) {
             $output->writeln(\N98\Util\Filesystem::humanFileSize($size));
         } else {
             $output->writeln("{$size}");
         }
     }
 }
All Usage Examples Of N98\Util\Filesystem::humanFileSize