Skip to content

Commit 8d92dd0

Browse files
authored
Merge pull request #2552 from thanhtaivtt/develop
Update File.php
2 parents 8939ea9 + 2acec9d commit 8d92dd0

3 files changed

Lines changed: 30 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
/**

user_guide_src/source/libraries/files.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ the results in kilobytes or megabytes, respectively::
6868
$kilobytes = $file->getSize('kb'); // 250.880
6969
$megabytes = $file->getSize('mb'); // 0.245
7070

71+
**getSizeByUnit()**
72+
73+
Returns the size of the uploaded file default in bytes. You can pass in either 'kb' or 'mb' as the first parameter to get
74+
the results in kilobytes or megabytes, respectively::
75+
76+
$bytes = $file->getSizeByUnit(); // 256901
77+
$kilobytes = $file->getSizeByUnit('kb'); // 250.880
78+
$megabytes = $file->getSizeByUnit('mb'); // 0.245
79+
7180
**getMimeType()**
7281

7382
Retrieve the media type (mime type) of the file. Uses methods that are considered as secure as possible when determining

0 commit comments

Comments
 (0)