Working with files and collecting their characteristics is a regular activity for a Laravel developer in web application development.
In this tutorial, we are going to guide you through the process of calculating the file size from a file path in Laravel 10. Knowing how to retrieve file sizes is vital whether you need to display file sizes to users, do calculations based on file sizes, or properly manage storage space.
Laravel provides few options to get file size from path. In this tutorial, we will use the concept of getting file size using Storage class and File Facade classes. We need to assume that we have few files inside /public folder or /storage/app/public folder.
Read More: How To Detect Device is Mobile or Desktop in Laravel 10
Let’s get started.
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
File Inside Folders (Few Example Cases)
Let’s say we have an image file inside these folders:
- /storage/app/public/images
- /public/images
We will see the concept of finding size using Storage class and File class in all these above cases.
Read More: How To Define Global Constant Variables in Laravel 10
How To Get File Size: Using Storage Class
Here, we will use Storage facade class and it’s size method.
Assume we have a file named as 1.png inside /storage/app/public/images folder.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; class SampleController extends Controller { public function index() { $fileSize = Storage::size('public/images/1.png'); // File size in bytes dd($fileSize); } }
Output
File size will be returned into bytes.
How To Get File Size: Using File Class
Here, we will use File facade class and it’s size method.
Assume we have a file named as 1.png inside /public/images folder.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\File; class SampleController extends Controller { public function index() { $fileSize = File::size(public_path('images/1.png')); // File size in bytes dd($fileSize); } }
Output
File size will be returned into bytes.
We hope this article helped you to learn How To Get File Size From File Path in Laravel 10 Tutorial in a very detailed way.
Read More: How To Create Custom Helper Function in Laravel 10 Tutorial
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.