Handling File Uploads in Laravel Livewire with Automatic Temporary Storage
Learn how to bind a file input to a Livewire property, validate it, and move it to a permanent disk while understanding the trade‑offs of temporary storage.
04 Dec 2025, 09:46 UTC

Problem: Repetitive boilerplate for Laravel file uploads
When building a typical Laravel form that accepts files, you often write a separate Form Request, manually validate the uploaded file, move it from PHP’s temporary directory to a storage disk, and then clean up the temp file. This pattern repeats across controllers and leads to duplicated code.
Thesis: Livewire simplifies the flow by binding the file to a component property
Livewire 2.0+ automatically stores an uploaded file in a temporary disk when you bind wire:model to a public property. You can then apply validation rules directly on that property, preview the file, and move it to its final location in a single save() method.
Worked example: Avatar upload component
Below is a minimal component that accepts an image, validates its MIME type and size, shows a preview, and stores the avatar on a disk named avatars.
// app/Http/Livewire/AvatarUpload.php
namespace App\Http\Livewire;
use Livewire\Component;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Storage;
class AvatarUpload extends Component
{
public $photo; // holds the uploaded file
protected function rules()
{
return [
'photo' => [
'required',
'image',
Rule::dimensions()->maxWidth(1000)->maxHeight(1000),
'max:2048', // 2 MB
],
];
}
public function save()
{
$this->validate();
// Generate a unique filename
$path = $this->photo->store('avatars', 'avatars'); // disk 'avatars'
// Optionally associate $path with a user model here
// auth()->user()->update(['avatar' => $path]);
$this->reset('photo'); // clear the input after success
$this->emit('avatarUpdated'); // notify parent if needed
}
public function render()
{
return view('livewire.avatar-upload');
}
}
The corresponding Blade view:
Choose avatar
@error('photo') {{ $message }} @enderror
@if ($photo)
@endif
Save
How the temporary storage works
When a user selects a file, Livewire streams it to the temporary disk configured under filesystems.disks.livewire-tmp (the default uses the local driver pointing to storage/framework/livewire-tmp). The file remains there until the component’s save() method runs, at which point you move it with Storage::disk('avatars')->put(...). After the request ends, Livewire automatically deletes the temporary file.
Trade‑offs and limitations
- Disk I/O: Every upload writes twice—once to the temporary disk and once to the final destination—potentially increasing load on busy servers.
- Component size: The file’s metadata (size, MIME type) is serialized into the Livewire payload; very large files can inflate the HTML response and affect performance.
- Cleanup reliance: If the request fails before reaching
save(), the temporary file is still removed by Livewire’s cleanup routine, but you should still schedule a periodic cleanup ofstorage/framework/livewire-tmpto guard against edge cases.
To mitigate these, ensure your php.ini values upload_max_filesize and post_max_size exceed the largest file you expect, and consider a cron job that removes files older than, say, 24 hours from the temporary directory.
Verification steps
- Create a fresh Laravel project (or use an existing one) and install Livewire:
composer require livewire/livewire. - Configure a filesystem disk named
avatarsinconfig/filesystems.phppointing tostorage/app/avatars(ensure the web server has write permission). - Generate the component:
php artisan livewire:make AvatarUploadand replace the generated files with the code above. - Add the component to a page:
<livewire:avatar-upload />. - Open the page in a browser, select an image under 2 MB, and click Save.
- Check that the file appears in
storage/app/avatarsand that no corresponding file remains instorage/framework/livewire-tmpafter the request.
If the file is not present in the final disk, verify the disk configuration and that the web server user can write to the target directory. If temporary files linger, inspect the Livewire version (must be ≥2.0) and check server logs for exceptions that might have prevented the cleanup routine from running.
Closing: When to reach for Livewire file uploads
Livewire’s automatic temporary storage removes much of the boilerplate associated with file handling, making it a good fit for inline forms such as profile avatars, product images, or document uploads where you already have a Livewire component. For massive files or high‑throughput APIs, a traditional request‑flow with dedicated Form Requests may still be preferable to avoid the double write and payload overhead.
By following the example above, you can implement a reliable upload flow with minimal code while being aware of the I/O and size implications. Test the flow in a staging environment, monitor the temporary directory, and adjust your PHP upload limits as needed.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.