PHP Luminova: HTTP File Downloader
Global helper functions are collections of procedural functions that provide commonly used functionality across different parts of your application enhancing code reusability and productivity.
The HTTP Downloader class, allows you to download files or data from different source.
Class Definition
- Class namespace:
\Luminova\Http\Downloader - This class is a Final class
Methods
response
Create a downloadable response.
Streams a file, resource, or raw string as an HTTP attachment.Supports large-file streaming, HTTP range requests.
Behavior:
- Detects source type (file, resource, string).
- Sends download headers and handles partial content (206).
- Streams content efficiently to the client.
public static response(
string|resource $source,
?string $filename = null,
array<string,mixed> $headers = [],
?string $etag = null
): \Luminova\Http\Message\Response<\Psr\Http\Message\ResponseInterface>Parameters:
| Parameter | Type | Description |
|---|---|---|
$source | string|resource | File path, open resource, or raw string. |
$filename | string|null | Download filename shown to the client. |
$headers | array<string,mixed> | Additional HTTP headers. |
$etag | string|null | Optional ETag for caching. |
Return Value:
\Luminova\Http\Message\Response<\Psr\Http\Message\ResponseInterface> - Returns PSR-7 response object.
Throws:
- Luminova\Exceptions\RuntimeException - When file source is missing or unreadable.
Examples:
use Luminova\Http\Header;
use Luminova\Http\Downloader;
// Download a file from path
$response = Downloader::response('/path/to/file.zip', 'download.zip');
// Send headers to browser
Header::send($response->getHeaders(), status $response->getStatusCode());
// Clear output buffers
Header::clearOutputBuffers();
// Use info for partial content download
$info = $response->getInfo(); // ['is_partial', 'offset', 'length', 'limit']
// Stream the body
echo $response->getBody();download
Send a downloadable response to the browser.
Streams a file, resource, or raw string as an HTTP attachment.Supports large-file streaming, HTTP range requests, and optionalchunked output with delay control.
Behavior:
- Detects source type (file, resource, string).
- Sends download headers and handles partial content (206).
- Streams content efficiently to the client.
- Optionally deletes files after successful transfer.
public static download(
\Psr\Http\Message\StreamInterface|string|resource $source,
?string $filename = null,
array<string,mixed> $headers = [],
bool $delete = false,
int $chunkSize = 8192,
int $delay = 0,
?string $etag = null
): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$source | \Psr\Http\Message\StreamInterface|string|resource | File path, open resource, stream object, or raw string. |
$filename | string|null | Download filename shown to the client. |
$headers | array<string,mixed> | Additional HTTP headers. |
$delete | bool | Delete file after download (files only). |
$chunkSize | int | Bytes per chunk when streaming. |
$delay | int | Microseconds delay between chunks. |
$etag | string|null | Optional ETag for caching. |
Return Value:
bool - Returns true on success, false on failure.
Throws:
- Luminova\Exceptions\RuntimeException - When file source is missing or unreadable.
Example:
use Luminova\Http\Downloader;
// Download a file from path
$status = Downloader::download('/path/to/file.zip', 'download.zip');