Download a file in Laravel using a URL to external resource

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Downloading Files from External Resources Using Laravel or Plain PHP Introduction: In this blog post, we will explore the process of downloading external files through Laravel or plain PHP. We'll discuss the limitations and workarounds to ensure seamless file downloads for your users, regardless of their location on a custom drive or external API. The Laravel Way: 1. The built-in response()->download() method works well with local files. However, it fails when you try to pass an external URL pointing to the actual file (like 'https://my-cdn.com/files/image.jpg'). This is because this method checks for the existence of the given path on your server, which obviously doesn't exist in our case. 2. To tackle this issue, you can use a third-party library like Guzzle or PHP Curl to download and return the file from the external URL as a direct response:
use GuzzleHttp\Client;

public function show() {
    $client = new Client();
    $response = $client->get('https://my-cdn.com/files/image.jpg');
    if ($response->getStatusCode() == 200) {
        return response($response->getBody(), 200, ['Content-Type' => 'image/jpeg']);
    } else {
        abort(404); // handle errors as needed
    }
}
In this example, the file is downloaded using Guzzle's get() method and the response->getBody() will contain its contents. The response is then returned to Laravel as a direct response with its proper content type and HTTP status code. Plain PHP: 1. If you don't prefer using frameworks, you can use plain PHP to download files from external resources without any limitations. This approach involves redirecting users to the actual external URL (like 'https://my-cdn.com/files/image.jpg') and then serving them the file directly:
header('Location: https://my-cdn.com/files/image.jpg');
exit();
In this case, you lose control over customizing the response header or content type for each downloaded file, but it does get the job done simply and effectively. Choosing the Best Approach: 1. When deciding which approach to use, consider your requirements and preferences. If you want more control over the response and better support from framework features, go with Laravel's built-in functionality or third-party libraries, such as Guzzle. Otherwise, plain PHP can handle file downloads efficiently without additional dependencies. 2. It is essential to test and ensure that your code is optimized for security, performance, and user experience when dealing with external resources. This will help prevent potential issues like unauthorized access or unwanted downloads. Conclusion: In this post, we have explored different approaches to downloading files from external resources using Laravel or plain PHP. While the framework offers a built-in method for local files, handling external URLs requires additional techniques with either Laravel's third-party libraries or plain PHP. Choosing the right approach depends on your requirements and preferences, but it is vital to maintain proper security and performance while delivering an optimal user experience.