Size.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Magento file size lib
  8. */
  9. namespace Magento\Framework\File;
  10. /**
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Size
  15. {
  16. /**
  17. * Data size converter
  18. *
  19. * @var \Magento\Framework\Convert\DataSize
  20. */
  21. private $dataSize;
  22. /**
  23. * Maximum file size for MAX_FILE_SIZE attribute of a form
  24. *
  25. * @link http://www.php.net/manual/en/features.file-upload.post-method.php
  26. * @var integer
  27. */
  28. protected static $_maxFileSize = -1;
  29. /**
  30. * Get post max size
  31. *
  32. * @return string
  33. */
  34. public function getPostMaxSize()
  35. {
  36. return $this->_iniGet('post_max_size');
  37. }
  38. /**
  39. * Get upload max size
  40. *
  41. * @return string
  42. */
  43. public function getUploadMaxSize()
  44. {
  45. return $this->_iniGet('upload_max_filesize');
  46. }
  47. /**
  48. * Get max file size in megabytes
  49. *
  50. * @param int $precision
  51. * @param int $mode
  52. * @return float
  53. */
  54. public function getMaxFileSizeInMb($precision = 0, $mode = \PHP_ROUND_HALF_DOWN)
  55. {
  56. return $this->getFileSizeInMb($this->getMaxFileSize(), $precision, $mode);
  57. }
  58. /**
  59. * Get file size in megabytes
  60. *
  61. * @param int $fileSize
  62. * @param int $precision
  63. * @param int $mode
  64. * @return float
  65. */
  66. public function getFileSizeInMb($fileSize, $precision = 0, $mode = \PHP_ROUND_HALF_DOWN)
  67. {
  68. return round($fileSize / (1024 * 1024), $precision, $mode);
  69. }
  70. /**
  71. * Get the maximum file size of the a form in bytes
  72. *
  73. * @return integer
  74. */
  75. public function getMaxFileSize()
  76. {
  77. if (self::$_maxFileSize < 0) {
  78. $postMaxSize = $this->getDataSize()->convertSizeToBytes($this->getPostMaxSize());
  79. $uploadMaxSize = $this->getDataSize()->convertSizeToBytes($this->getUploadMaxSize());
  80. $min = max($postMaxSize, $uploadMaxSize);
  81. if ($postMaxSize > 0) {
  82. $min = min($min, $postMaxSize);
  83. }
  84. if ($uploadMaxSize > 0) {
  85. $min = min($min, $uploadMaxSize);
  86. }
  87. self::$_maxFileSize = $min;
  88. }
  89. return self::$_maxFileSize;
  90. }
  91. /**
  92. * Converts a ini setting to a integer value
  93. *
  94. * @deprecated 100.1.0 Please use \Magento\Framework\Convert\DataSize
  95. *
  96. * @param string $size
  97. * @return integer
  98. */
  99. public function convertSizeToInteger($size)
  100. {
  101. return $this->getDataSize()->convertSizeToBytes($size);
  102. }
  103. /**
  104. * Gets the value of a configuration option
  105. *
  106. * @link http://php.net/manual/en/function.ini-get.php
  107. * @param string $param The configuration option name
  108. * @return string
  109. */
  110. protected function _iniGet($param)
  111. {
  112. return trim(ini_get($param));
  113. }
  114. /**
  115. * The getter function to get the new dependency for real application code
  116. *
  117. * @return \Magento\Framework\Convert\DataSize
  118. *
  119. * @deprecated 100.1.0
  120. */
  121. private function getDataSize()
  122. {
  123. if ($this->dataSize === null) {
  124. $this->dataSize =
  125. \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Convert\DataSize::class);
  126. }
  127. return $this->dataSize;
  128. }
  129. }