Laravel File Storage: A Beginner’s Guide

ArjunAmrutiya
3 min readJul 20, 2023

--

Laravel File Storage

Introduction:
In web development, handling file uploads and storage is a common requirement. Laravel, one of the most popular PHP frameworks, provides a convenient and efficient way to handle file storage through its built-in file storage system. In this blog post, we will explore how to use Laravel’s file storage capabilities using the “storage” disk, along with practical examples to help beginners understand the concepts.

Prerequisites:
Before diving into file storage, make sure you have Laravel installed and a basic understanding of Laravel’s file system configuration.

Configuring File Storage in Laravel: Laravel’s file storage is configured in the config/filesystems.php file. By default, Laravel includes a "local" disk configuration for storing files on the local file system. Additionally, it includes a "public" disk for serving files directly to users. We will focus on the "local" disk in this blog.

Using the “local” Disk for File Storage: The “local” disk in Laravel utilizes the local file system to store files. To start using it, follow these steps:

Step 1: Storing Files: To store a file using Laravel’s file storage, you need to utilize the Storage facade, which provides a clean and consistent API. Here's an example of how to store a file:

use Illuminate\Support\Facades\Storage;

$file = $request->file('file'); // Retrieve the uploaded file from the request
$filename = $file->getClientOriginalName(); // Retrieve the original filename

Storage::disk('local')->put('path/to/store/'.$filename, file_get_contents($file));

In the example above, we retrieve the uploaded file from the request, obtain the original filename, and use the put() method to store the file in the desired location within the "local" disk. The file_get_contents() function is used to read the contents of the file and pass it to the put() method.

Step 2: Retrieving Files: To retrieve a file from the “local” disk, you can use the url() method provided by the Storage facade. Here's an example:

use Illuminate\Support\Facades\Storage;

$fileUrl = Storage::disk('local')->url('path/to/store/'.$filename);

return response()->json(['file_url' => $fileUrl]);

The url() method generates a public URL for the specified file, which can be used to access the file through a web browser or include it in a response.

Step 3: Deleting Files: To delete a file from the “local” disk, you can use the delete() method provided by the Storage facade. Here's an example:

use Illuminate\Support\Facades\Storage;

Storage::disk('local')->delete('path/to/store/'.$filename);

The delete() method removes the specified file from the disk.

Conclusion: Laravel’s file storage system offers a simple and powerful way to handle file uploads and storage in your web applications. By utilizing the “local” disk, you can easily store, retrieve, and delete files. In this blog post, we covered the basics of file storage using Laravel’s built-in file system capabilities. By experimenting with the provided examples, beginners can gain a better understanding of how to leverage Laravel’s file storage to handle file uploads in their projects.

Remember to explore the Laravel documentation for more advanced features and additional disk configurations like Amazon S3, FTP, or custom drivers to suit your specific needs. Happy coding!

--

--

ArjunAmrutiya

👋 Hey there! I'm Arjun Amrutiya, a passionate web developer and blogger who loves all things PHP, Laravel and Vue.js. Welcome to my Medium account!