Search by

spline / tus

splinephp

PSR-7 native server for the TUS resumable upload protocol

v0.1.0 2026-11-29 21:44 UTC

This package is auto-updated.

Last update: 2026-11-29 21:47:19 UTC


README

License: MIT codecov

A PHP implementation of the TUS resumable upload protocol.

Installation

composer require spline/tus

Requirements

  • PHP 8.2 or higher
  • A PSR-7 HTTP message implementation and a PSR-17 response factory (for example nyholm/psr7, guzzlehttp/psr7, laminas/laminas-diactoros or spline/http)

Usage

use Spline\Tus\TusServer;
use Nyholm\Psr7\Factory\Psr17Factory; // any PSR-17 ResponseFactoryInterface

$uploadDir = '/path/to/uploads';
$maxSize = 1024 * 1024 * 100; // 100MB
$chunkSize = 1024 * 1024 * 5; // 5MB

$server = new TusServer(new Psr17Factory(), $uploadDir, $maxSize, $chunkSize);

// Handle a PSR-7 server request and get a PSR-7 response back
$response = $server->handleRequest($request);

The server builds every response through the factory you pass in, so it works with whichever PSR-7 implementation your application already uses.

Events

Pass any PSR-14 dispatcher and the server reports what happens to each upload. Without a dispatcher nothing is dispatched.

use Spline\Tus\Event\UploadCompleted;
use Spline\Tus\Event\UploadCreated;

$server->setEventDispatcher($dispatcher);

// Symfony's dispatcher shown; any PSR-14 implementation works the same way.
$dispatcher->addListener(UploadCreated::class, function (UploadCreated $event): void {
    if (!str_starts_with($event->cache->metadata['filetype'] ?? '', 'image/')) {
        $event->reject('Only images are accepted'); // server answers 403, writes nothing
    }
});

$dispatcher->addListener(UploadCompleted::class, function (UploadCompleted $event): void {
    // $event->path is the finished file inside the upload directory, under the
    // name it was uploaded as. $event->mimeType is sniffed from its contents:
    // use it, not the client's `filetype`, to pick the final extension.
    $queue->dispatch(new ProcessUpload($event->path, $event->mimeType, $event->cache->metadata));
});
Event When Payload
UploadCreated Creation request validated, before anything is written fileName, path, cache, request; reject()
UploadProgressed Bytes appended by a PATCH or creation-with-upload request fileName, path, cache, offset
UploadCompleted All bytes received, checks passed, metadata container removed fileName, path, cache, mimeType
UploadTerminated DELETE request removed the upload fileName, path, cache (nullable)

Features

  • TUS protocol v1.0.0 compliant
  • Resumable file uploads
  • Checksum verification support (md5, sha1, sha256, sha512)
  • Configurable storage backends
  • MIME type validation, sniffed from the file's contents; finished files are never renamed
  • Automatic cleanup of expired uploads

License

MIT