Laravel - get size from uploaded file
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Retrieving File Size from Uploaded Files in Laravel
In Laravel, handling uploaded files is quite straightforward thanks to the built-in functionality for file storage and management. However, if you need more context about your files such as their size, there are a couple of ways you can achieve it. This article will provide you with a thorough answer on how to get file sizes from uploaded files in Laravel.
1. Using the built-in Storage Facade:
The Storage facade comes with a handy method called `disk`. You can use this to retrieve information about specific files or directories stored within your storage system. For example, you can get the size of an uploaded file as follows:
$file = Storage::disk('public')->get($path);
$sizeInBytes = Storage::disk('public')->size($file->getPathname());
Here, we first retrieve the file using the get method and then call the `size` method to return its size in bytes. Once you have this value, it's easy to convert it into a more readable format, such as kilobytes or megabytes.
2. Using PHP functions:
If you prefer using built-in PHP functions for handling files, you can use the `filesize` function. This will directly give you the file size in bytes and doesn't require interacting with storage. However, it's important to remember that using this method might not be compatible with Laravel's file upload management process since it assumes the file is already stored on your server.
$filePath = $file->getPathname();
$sizeInBytes = filesize($filePath);
3. Using Blade View:
If you want to display the file size in a view, you'll need to use HTML to format the value accordingly. Here is an example of how it could be done:
<p>File Size: <span class="file-size" data-bytes="{{ $file->getSize() }}"></span></p>
4. Converting File Size to Readable Format:
To display file sizes in a user-friendly format (kilobytes or megabytes), you can use PHP's built-in formatting functions like `round()` and `number_format()`. Here is an example for displaying the size in kilobytes:
$fileSizeInBytes = $file->getSize();
$formattedKB = round(($fileSizeInBytes / 1024), 2);
echo "File Size: " . number_format($formattedKB, 2) . " KB";
5. Important Tips for File Uploads:
To ensure that you can effectively handle file uploads in your application, consider the following tips:
1. Always validate user input before processing or saving it to prevent security vulnerabilities and unexpected behavior.
2. Use reliable storage systems like Amazon S3, Google Cloud Storage, or digitalOcean Spaces for improved performance, scalability, and data protection.
3. For larger files, split them into smaller chunks or use streaming techniques to reduce memory consumption on the server.
In summary, to get file size from uploaded files in Laravel, you can either utilize the built-in Storage facade, PHP functions, or Blade view syntax. By following best practices and ensuring proper validation and storage handling, your web application will effectively manage and present file information to users.