chubbyphp / chubbyphp-static-file
A minimal static file middleware for PSR 15.
Package info
github.com/chubbyphp/chubbyphp-static-file
pkg:composer/chubbyphp/chubbyphp-static-file
1.4.6
2026-09-08 19:44 UTC
Requires
- php: ^8.3
- psr/http-factory: ^1.1
- psr/http-message: ^1.1|^2.0
- psr/http-server-handler: ^1.0.2
- psr/http-server-middleware: ^1.0.2
Requires (Dev)
- chubbyphp/chubbyphp-dev-helper: dev-master
- chubbyphp/chubbyphp-mock: ^2.2.2
- infection/infection: ^0.35.4
- php-coveralls/php-coveralls: ^2.9.1
- phpstan/extension-installer: ^1.4.3
- phpstan/phpstan: ^2.2.13
- phpunit/phpunit: ^12.5.34
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Description
A minimal PSR-15 middleware that serves static files from a public directory.
- Serves only regular, readable files inside the public directory; path traversal and symlinks pointing outside are rejected.
- Handles
GETandHEADrequests, any other method is passed to the next handler. - Sends
Content-Type(based on the file extension,application/octet-streamas fallback),Content-Length,ETagandX-Content-Type-Options: nosniff. - Responds with
304 Not Modifiedif theIf-None-Matchheader matches the file'sETag. - Passes the request to the next handler if no matching file exists.
Requirements
- php: ^8.3
- psr/http-factory: ^1.1
- psr/http-message: ^1.1|^2.0
- psr/http-server-handler: ^1.0.2
- psr/http-server-middleware: ^1.0.2
Installation
Through Composer as chubbyphp/chubbyphp-static-file.
composer require chubbyphp/chubbyphp-static-file "^1.4"
Usage
Register the middleware before the routing of your PSR-15 based framework, so that static files are served without hitting a route.
<?php declare(strict_types=1); namespace App; use Chubbyphp\StaticFile\StaticFileMiddleware; use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\StreamFactoryInterface; /** @var ResponseFactoryInterface $responseFactory */ $responseFactory = ...; /** @var StreamFactoryInterface $streamFactory */ $streamFactory = ...; $app = ...; $app->add(new StaticFileMiddleware( $responseFactory, $streamFactory, __DIR__ . '/public' ));
Options
The constructor accepts two optional arguments:
$hashAlgorithm(default:md5): the algorithm used to calculate theETag, must be supported by hash_algos().$mimetypes(default: bundled list based on the Apache mime.types, regenerate withphp generate-mimetypes.php): a map of file extension to mime type.
$app->add(new StaticFileMiddleware( $responseFactory, $streamFactory, __DIR__ . '/public', 'sha256', ['css' => 'text/css', 'js' => 'text/javascript', 'png' => 'image/png'] ));
Copyright
2026 Dominik Zogg