Unable to retrieve the file_size for file when using Storage::fake for testing
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Unable to Retrieve File Size for Files When Using Storage::fake for Testing - A Comprehensive Solution
Body:
Introduction: Recently, you experienced an issue with retrieving file_size for files when using Storage::fake in your Laravel application. You have upgraded your app from Laravel v8 to v9 and are facing this problem. This article aims to provide a detailed explanation of the issue along with potential solutions to resolve it.
Background: The File System Flysystem library is used by Laravel for handling files. It was updated to version 3, which may cause some unexpected behavior if not properly implemented or understood. One such issue could be when you use Storage::fake to simulate file uploads during testing.
The Issue: In your code, the following statement throws an error: "League\Flysystem\UnableToRetrieveMetadata : Unable to retrieve the file_size for file at location: test_file.xlsx." You believe it may be related to the recent Flysystem update, but this issue doesn't seem to be directly related to your application.
Possible Cause and Solutions: The cause of this error could be that Flysystem is trying to retrieve the file_size for a non-existing file during testing or in an unsupported environment. You can try these solutions to fix the issue:
1. Revert to older versions of Laravel and related packages: If you have recently upgraded your application, try reverting to the previous version of Laravel and its dependencies (including Flysystem). This will prevent any unexpected changes introduced by the update from causing issues in your project.
2. Ensure you're using Storage::fake correctly: Make sure that you are setting up the mock files accordingly before testing. You can use UploadedFile::fake() to create fake uploaded files and simulate file uploads during tests.
3. Modify your code for specific test cases: If you know your application doesn't particularly need to retrieve file_size, you could modify the relevant code or tests accordingly to avoid relying on this information. This may provide a temporary solution depending on your project's requirements and scope.
4. Investigate any potential conflicts with other packages: Ensure that you are not using incompatible libraries or dependencies that may cause issues with file_size retrieval during testing. Updating to the latest stable versions of these dependencies could resolve the problem, assuming no other underlying issues are present.
Conclusion: The recent Flysystem update could be causing your issue with retrieving file_size for files when using Storage::fake in your Laravel application. Carefully evaluate the suggested solutions based on your project's specific requirements and try to determine if any of these options resolve your problem. Remember, always back up your code before making changes and consult with fellow developers or online resources whenever you encounter issues like this one.