Get the size of a file or a directory using Javascript, Typescript, or Php
In the realm of software development, where efficiency and optimization are paramount, paying attention to the size of files and directories is more crucial than ever. As developers, understanding the implications of file size can greatly impact the performance, storage, and overall user experience of our applications. In this blog post, we'll find out how to obtain the size of any file or directory (and all its contents) with a single line of code. The TurboDepot library FilesManager class has a method that does this calculation for us with the fastest algorithm possible. Let's see how to do it in javascript, typescript, or PHP:
Get the file or directory size with PHP
Download the latest TurboCommons and TurboDepot phar files, place them on your project as dependencies and run the following code:
require 'path/to/your/dependencies/folder/turbocommons-php-X.X.X.phar';
require 'path/to/your/dependencies/folder/turbodepot-php-X.X.X.phar';
use org\turbodepot\src\main\php\managers\FilesManager;
$filesManager = new FilesManager();
$filesManager->getFileSize('path/to/file1');
$filesManager->getDirectorySize('path/to/directory');
getFileSize() will return the size of the provided file in bytes. getDirectorySize() will return the size of the provided directory and all its contents in bytes.
Get the file or directory size with Javascript (node js)
Install the turbodepot dependency by executing the following command at the root of your project:
npm install turbodepot-node
And then run the following code:
const { FilesManager } = require('turbodepot-node');
let filesManager = new FilesManager();
filesManager.getFileSize('path/to/file1');
filesManager.getDirectorySize('path/to/directory');
getFileSize() will return the size of the provided file in bytes. getDirectorySize() will return the size of the provided directory and all its contents in bytes.
Get the file or directory size with Typescript (TS)
Install the turbodepot dependency by executing the following command at the root of your project:
npm install turbodepot-node
And then run the following code:
import { FilesManager } from 'turbodepot-node';
let filesManager = new FilesManager();
filesManager.getFileSize('path/to/file1');
filesManager.getDirectorySize('path/to/directory');
getFileSize() will return the size of the provided file in bytes. getDirectorySize() will return the size of the provided directory and all its contents in bytes.