Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add a File::bytesToHuman() helper #48827

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Illuminate/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -789,4 +789,22 @@ public function cleanDirectory($directory)
{
return $this->deleteDirectory($directory, true);
}

/**
* Format the number of bytes to a human-readable string.
*
* @param int $bytes
* @param int $precision
* @return string
*/
public function bytesToHuman($bytes, $precision = 2)
{
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

for ($i = 0; ($bytes / 1024) > 0.9 && ($i < count($units) - 1); $i++) {
$bytes /= 1024;
}

return sprintf('%s %s', round($bytes, $precision), $units[$i]);
}
}
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* @method static bool deleteDirectory(string $directory, bool $preserve = false)
* @method static bool deleteDirectories(string $directory)
* @method static bool cleanDirectory(string $directory)
* @method static string bytesToHuman(int $bytes, int $precision = 2)
* @method static \Illuminate\Filesystem\Filesystem|mixed when(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null)
* @method static \Illuminate\Filesystem\Filesystem|mixed unless(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null)
* @method static void macro(string $name, object|callable $macro)
Expand Down
18 changes: 18 additions & 0 deletions tests/Filesystem/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,24 @@ public function testHash()
$this->assertSame('76d3bc41c9f588f7fcd0d5bf4718f8f84b1c41b20882703100b9eb9413807c01', $filesystem->hash(self::$tempDir.'/foo.txt', 'sha3-256'));
}

public function testBytesToHuman()
{
$filesystem = new Filesystem;

$this->assertSame('0 B', $filesystem->bytesToHuman(0));
$this->assertSame('1 B', $filesystem->bytesToHuman(1));
$this->assertSame('1 KB', $filesystem->bytesToHuman(1024));
$this->assertSame('2 KB', $filesystem->bytesToHuman(2048));
$this->assertSame('1.23 KB', $filesystem->bytesToHuman(1264));
$this->assertSame('1.234 KB', $filesystem->bytesToHuman(1264, 3));
$this->assertSame('5 GB', $filesystem->bytesToHuman(1024 * 1024 * 1024 * 5));
$this->assertSame('10 TB', $filesystem->bytesToHuman((1024 ** 4) * 10));
$this->assertSame('10 PB', $filesystem->bytesToHuman((1024 ** 5) * 10));
$this->assertSame('1 ZB', $filesystem->bytesToHuman(1024 ** 7));
$this->assertSame('1 YB', $filesystem->bytesToHuman(1024 ** 8));
$this->assertSame('1024 YB', $filesystem->bytesToHuman(1024 ** 9));
}

/**
* @param string $file
* @return int
Expand Down