Luminova Framework

PHP Luminova: HTTP File Downloader

Last updated: 2025-12-28 09:11:45

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:

ParameterTypeDescription
$sourcestring|resourceFile path, open resource, or raw string.
$filenamestring|nullDownload filename shown to the client.
$headersarray<string,mixed>Additional HTTP headers.
$etagstring|nullOptional ETag for caching.

Return Value:

\Luminova\Http\Message\Response<\Psr\Http\Message\ResponseInterface> - Returns PSR-7 response object.

Throws:

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
): bool

Parameters:

ParameterTypeDescription
$source\Psr\Http\Message\StreamInterface|string|resourceFile path, open resource, stream object, or raw string.
$filenamestring|nullDownload filename shown to the client.
$headersarray<string,mixed>Additional HTTP headers.
$deleteboolDelete file after download (files only).
$chunkSizeintBytes per chunk when streaming.
$delayintMicroseconds delay between chunks.
$etagstring|nullOptional ETag for caching.

Return Value:

bool - Returns true on success, false on failure.

Throws:

Example:

use Luminova\Http\Downloader;

// Download a file from path
$status = Downloader::download('/path/to/file.zip', 'download.zip');