coercive / fileserve
Coercive FileServe Utility
2.1.0
2026-10-19 17:47 UTC
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: ^12
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
PHP Serve File with header : inline, download, X-Sendfile, and byte ranges (single and multiple) for resumable downloads and video / audio seeking.
Compatible PHP 7.4 → 8.5, no dependency.
Get
composer require coercive/fileserve
Usage
use Coercive\Utility\FileServe\FileServe; # Throws an Exception if the file does not exist, is not a regular file, or if the path contains a ".." segment $serve = new FileServe('/path/file.extension'); # Serve file (the optional name is only used to detect the mime type) $serve->serve(); $serve->serve('name.pdf'); # Send download (attachment, with an optional custom file name, UTF-8 allowed) $serve->download(); $serve->download(true, 'My file.pdf'); # Display in the browser with a file name (inline) $serve->download(false, 'My file.pdf'); # Send range : 206 Partial Content for single or multiple ranges (multipart/byteranges), If-Range supported # Without Range header, the whole file is sent # Throws an Exception after sending a 416 Range Not Satisfiable $serve->range(); # Let Apache send the file (mod_xsendfile) $serve->XSendFile(); $serve->XSendFile(true, 'My file.pdf'); # Get mime : by the extension only, the content of the file is never read # "unknown/{extension}" if not found (the header sent is then application/octet-stream) $string = $serve->mimeType(); $string = $serve->mimeType('other.pdf'); # Get filesize $int = $serve->getSize(); # Get etag (without quotes) $string = $serve->etag();
HEAD requests receive the headers only.
Options
# Enable : Last-Modified + ETag headers, and 304 Not Modified response (If-None-Match / If-Modified-Since) # Disable (default) : header no cache options $serve->enableCache(); $serve->disableCache(); # Enable / Disable header content disposition filename for html5 attr : <a download="filename"> $serve->enableFilename(); $serve->disableFilename(); # Custom Content-Type $serve->setHeaderContentType('application/json; charset=utf-8');
Mime types
use Coercive\Utility\FileServe\MimeTypes; # Embedded extension to mime type mapping (null if unknown) $string = MimeTypes::get('pdf');
Tests
composer install
vendor/bin/phpunit
PHPUnit 12 requires PHP 8.3+ to run the tests ; the library itself still runs on PHP 7.4.
The http group starts the PHP built-in server on a free local port : vendor/bin/phpunit --exclude-group http to skip it.