Download Files Easily with Laravel’s HTTP sink Method

Harris Raftopoulos
1 min readJan 14, 2025

Need to download files with HTTP requests? Laravel’s sink method provides a clean way to save HTTP responses directly to files with minimal code.

Basic Usage

Save HTTP responses directly to files:

Http::sink(storage_path('download.zip'))
->get('https://example.com/example-file.zip');

Real-World Example

Here’s how you might use it in a download manager:

class DownloadManager
{
public function downloadFile(string $url, string $filename)
{
return Http::sink(storage_path("downloads/{$filename}"))
->withHeaders([
'User-Agent' => 'MyApp/1.0',
'Accept' => '*/*'
])
->get($url);
}

public function downloadBackup(string $backupUrl, string $apiKey)
{
$timestamp = now()->format('Y-m-d-His');

return Http::sink(storage_path("backups/backup-{$timestamp}.zip"))
->withToken($apiKey)
->get($backupUrl);
}

public function downloadReportAsCsv(string $reportUrl)
{
return Http::sink(
storage_path('reports/latest.csv'),
withHeaders([
'Accept' => 'text/csv'
])
)->get($reportUrl);
}
}

The sink method simplifies file downloads by handling the file writing process for you.

If this guide was helpful to you, subscribe to my daily newsletter and give me a follow on X/Twitter and Bluesky. It helps a lot!

--

--

Harris Raftopoulos
Harris Raftopoulos

Written by Harris Raftopoulos

Senior Software Engineer | 15+ years in PHP, 11+ with Laravel | Expert in Livewire, TailwindCSS & VueJS | DevOps Enthusiast | 🎤Speaker at Athens Laravel Meetup

Responses (2)