PHP code example of moudarir / downloader

1. Go to this page and download the library: Download moudarir/downloader library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

moudarir / downloader example snippets


$response = Download::fromFile('/path/to/file.pdf', mime: MimeType::PDF);

$response->send();

$response = Download::fromFile('/path/to/file.pdf', 'document.pdf', MimeType::PDF);

$response->send();

$response = Download::fromFile('/path/to/file.pdf', mime: MimeType::PDF)->inline();

$response->send();

$response = Download::fromFile('/path/to/video.mp4', 'video.mp4', MimeType::MP4, ResponseAction::PARTIAL)
    ->inline();

$response->send();

$response = Download::fromData($data, 'document.txt', 'text/plain');

$response->send();

use Moudarir\Downloader\Download;
use Moudarir\Downloader\Enums\ResponseAction;

$response = Download::fromFile('/path/to/file.pdf', responseAction: ResponseAction::X_SEND_FILE);

$response->send();

use Moudarir\Downloader\Download;
use Moudarir\Downloader\Enums\ResponseAction;

$response = Download::fromFile(
    '/path/to/file.pdf',
    responseAction: ResponseAction::X_ACCEL_REDIRECT,
    xAccelRedirectUri: '/protected/path/to/file.pdf'
);

$response->send();

use Moudarir\Downloader\Download;
use Moudarir\Downloader\DownloadConfig;

$config = new DownloadConfig()
    ->withLimitRate(500 * 1024)
    ->withChunkSize(256 * 1024)
    ->withMaxRangeItems(5);

$response = Download::fromFile(
    '/path/to/video.mp4',
    'video.mp4',
    MimeType::MP4,
    config: $config,
);

$response->send();

$response->send();

use Moudarir\Downloader\Enums\StatusCode;
use Moudarir\File\Enum\MimeType;

$response = Download::fromFile('/path/to/file.pdf', mime: MimeType::PDF);

$metadata = $response->metadata();

echo $metadata->statusCode()->value;
echo $metadata->contentLength();
echo $metadata->contentType();
echo $metadata->filename();
echo $metadata->etagValue();

if ($metadata->statusCode() === StatusCode::OK) {
    // ...
}

$response->send();

use Moudarir\Downloader\Download;
use Moudarir\Downloader\Enums\ETagStrategy;

$response = Download::fromFile('/path/to/file.pdf', strategy: ETagStrategy::SHA256);

$response->send();