By default, Laravel provides an easy-to-use file upload functionality using the Storage
facade. Before you start, ensure that your application’s file system configuration is set up.
Configure File System: Open the config/filesystems.php
file to configure your file system. Laravel supports local, public, and cloud-based storage.
Example:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => public_path('uploads'),
'url' => env('APP_URL').'/uploads',
'visibility' => 'public',
],
],
In your Blade template, create an HTML form to allow users to select and upload a file. Make sure to set the enctype
attribute to multipart/form-data
for file uploads.
Example (Upload Form):
<form action="{{ route('file.upload') }}" method="POST" enctype="multipart/form-data">
@csrf
<input type="file" name="file">
<button type="submit">Upload File</button>
</form>
@csrf
: Include a CSRF token for security.enctype="multipart/form-data"
: Specifies that the form will handle file uploads.In your controller, use Laravel's Request
to handle the file upload. The file()
method of the request will give you access to the uploaded file.
Example (Controller Method):
use Illuminate\Http\Request;
public function uploadFile(Request $request)
{
// Validate the uploaded file
$request->validate([
'file' => 'required|file|mimes:jpg,png,pdf|max:2048',
]);
// Store the file in the 'public' disk
$filePath = $request->file('file')->store('uploads', 'public');
// Return the path or store it in the database
return back()->with('file', $filePath);
}
validate()
: Validates the file upload (required, file type, size).store()
: Stores the file in the specified directory (e.g., uploads
) on the disk (e.g., public
).Once the file is uploaded, you may want to display it or provide a link to the user. You can use the asset()
function to generate the URL for the uploaded file.
Example (Displaying Uploaded File):
<img src="{{ asset('storage/'.$filePath) }}" alt="Uploaded File">
Laravel provides several methods to interact with the uploaded files, such as retrieving or deleting them.
Retrieve a File:
$path = storage_path('app/'.$filePath); // Get the file path
$fileContents = file_get_contents($path); // Retrieve file content
You can apply additional validation rules based on the file’s type, size, and extension.
Example (File Validation):
$request->validate([
'file' => 'required|file|mimes:jpg,png,jpeg,pdf|max:2048', // Max size: 2MB
]);
Laravel also supports uploading multiple files at once. You can use the array
notation in the form and handle the upload accordingly.
Example (Multiple File Upload Form):
<form action="{{ route('files.upload') }}" method="POST" enctype="multipart/form-data">
@csrf
<input type="file" name="files[]" multiple>
<button type="submit">Upload Files</button>
</form>
Laravel simplifies file uploads by providing intuitive methods to handle file validation, storage, and retrieval. With features like disk configuration, validation rules, and file retrieval, you can easily manage file uploads in your Laravel applications. Whether you are handling a single file or multiple files, Laravel makes it easy to implement file uploading securely and efficiently.