Skip to content

Commit 3f9ed80

Browse files
committed
Fix bug #2476
1 parent 5d472ea commit 3f9ed80

2 files changed

Lines changed: 21 additions & 14 deletions

File tree

system/Files/File.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,29 +86,36 @@ public function __construct(string $path, bool $checkFile = false)
8686
* the file in the $_FILES array if available, as PHP calculates this based
8787
* on the actual size transmitted.
8888
*
89-
* @param string $unit The unit to return:
90-
* - b Bytes
91-
* - kb Kilobytes
92-
* - mb Megabytes
93-
*
94-
* @return integer|null The file size in bytes or null if unknown.
89+
* @return integer The file size in bytes
9590
*/
96-
public function getSize(string $unit = 'b')
91+
public function getSize()
9792
{
9893
if (is_null($this->size))
9994
{
100-
$this->size = filesize($this->getPathname());
95+
$this->size = parent::getSize();
10196
}
10297

98+
return $this->size;
99+
}
100+
101+
/**
102+
* Retrieve the file size by unit.
103+
*
104+
* @param string $unit
105+
*
106+
* @return integer|string
107+
*/
108+
public function getSizeByUnit(string $unit = 'b')
109+
{
103110
switch (strtolower($unit))
104111
{
105112
case 'kb':
106-
return number_format($this->size / 1024, 3);
113+
return number_format($this->getSize() / 1024, 3);
107114
case 'mb':
108-
return number_format(($this->size / 1024) / 1024, 3);
115+
return number_format(($this->getSize() / 1024) / 1024, 3);
116+
default:
117+
return $this->getSize();
109118
}
110-
111-
return (int) $this->size;
112119
}
113120

114121
//--------------------------------------------------------------------

tests/system/Files/FileTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ public function testGetSizeReturnsKB()
5151
{
5252
$file = new File(SYSTEMPATH . 'Common.php');
5353
$size = number_format(filesize(SYSTEMPATH . 'Common.php') / 1024, 3);
54-
$this->assertEquals($size, $file->getSize('kb'));
54+
$this->assertEquals($size, $file->getSizeByUnit('kb'));
5555
}
5656

5757
public function testGetSizeReturnsMB()
5858
{
5959
$file = new File(SYSTEMPATH . 'Common.php');
6060
$size = number_format(filesize(SYSTEMPATH . 'Common.php') / 1024 / 1024, 3);
61-
$this->assertEquals($size, $file->getSize('mb'));
61+
$this->assertEquals($size, $file->getSizeByUnit('mb'));
6262
}
6363

6464
/**

0 commit comments

Comments
 (0)