PHPackages                             finn1990/tus-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. finn1990/tus-php

ActiveLibrary[HTTP &amp; Networking](/categories/http)

finn1990/tus-php
================

A pure PHP server and client for the tus resumable upload protocol v1.0.0

06PHP

Since May 11Pushed 8y ago1 watchersCompare

[ Source](https://github.com/finn1990/tus-php)[ Packagist](https://packagist.org/packages/finn1990/tus-php)[ RSS](/packages/finn1990-tus-php/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (0)

> ALERT: The project is still in its initial stage. Some implementations might change in the future. Feel free to try and report any issues. Pull requests and project recommendations are more than welcome!

Tus PHP
=======

[](#tus-php)

[![PHP Version](https://camo.githubusercontent.com/faa5b0af6d4fbc2cdec5ca79a797a378d7d698cc412c862aeef9897edd32e647/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372e312e332532422d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ankitpokhrel/tus-php)[![Build](https://camo.githubusercontent.com/353562f463237e92ce98de9e4732630921e9a4d79e099759da7e53431ef83c5d/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f616e6b6974706f6b6872656c2f7475732d7068702f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/ankitpokhrel/tus-php)[![Code Coverage](https://camo.githubusercontent.com/28d9fe6d65ba8694c86d481cc18851918b62f888c43c7d5ac32d9517aa277c78/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f616e6b6974706f6b6872656c2f7475732d7068702e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/ankitpokhrel/tus-php/)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/254cc6842b207a4b20e07166751777ce3a3946f274181e95889a6c1f909c11c3/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f616e6b6974706f6b6872656c2f7475732d7068702e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/ankitpokhrel/tus-php/)[![Download](https://camo.githubusercontent.com/6a70ea0e8b880c38b193a95f9d0629e44caba164cb484d46c1edb8ecf9078ee5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616e6b6974706f6b6872656c2f7475732d7068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ankitpokhrel/tus-php)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/ankitpokhrel/tus-php/blob/master/LICENSE)

*Resumable file upload in PHP using [tus resumable upload protocol v1.0.0](https://tus.io)*.

### Overview

[](#overview)

tus is a HTTP based protocol for resumable file uploads. Resumable means you can carry on where you left off without re-uploading whole data again in case of any interruptions. An interruption may happen willingly if the user wants to pause, or by accident in case of a network issue or server outage.

[![Tus PHP demo](https://github.com/ankitpokhrel/tus-php/raw/master/example/demo.gif)](https://github.com/ankitpokhrel/tus-php/blob/master/example/demo.gif)

### Resources

[](#resources)

1. [Medium Article](https://medium.com/@ankitpokhrel/resumable-file-upload-in-php-handle-large-file-uploads-in-an-elegant-way-e6c6dfdeaedb)
2. [Laravel/Lumen Integration](https://github.com/ankitpokhrel/tus-php/wiki/Laravel-&-Lumen-Integration)

### Installation

[](#installation)

Pull the package via composer.

```
$ composer require ankitpokhrel/tus-php:dev-master
```

Usage
-----

[](#usage)

#### Server

[](#server)

This is how a simple server looks like.

```
// server.php

$server   = new \TusPhp\Tus\Server('redis'); // Leave empty for file based cache
$response = $server->serve();

$response->send();

exit(0); // Exit from current PHP process.
```

You need to rewrite your server to respond to a specific endpoint. For example:

###### Nginx

[](#nginx)

```
# nginx.conf

location /files {
    try_files $uri $uri/ /server.php?$query_string;
}
```

###### Apache

[](#apache)

```
# .htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^files/?(.*)?$ /server.php/$1 [QSA,L]
```

Default max upload size is 0 which means there is no restriction. You can set max upload size as described below.

```
$server->setMaxUploadSize(100000000); // 100 mb in bytes
```

#### Client

[](#client)

The client can be used for creating, resuming and/or deleting uploads.

```
$client = new \TusPhp\Tus\Client($baseUrl, 'redis'); // Leave second parameter empty for file based cache

// Optional. If a key is not set explicitly, the system will generate a unique uuid.
$key = 'your unique key';

$client->setKey($key)->file('/path/to/file', 'filename.ext');

// Create and upload a chunk of 1mb
$bytesUploaded = $client->upload(1000000);

// Resume, $bytesUploaded = 2mb
$bytesUploaded = $client->upload(1000000);

// To upload whole file, skip length param
$client->file('/path/to/file', 'filename.ext')->upload();
```

To check if the file was partially uploaded before, you can use `getOffset` method. It returns false if the upload isn't there or invalid, returns total bytes uploaded otherwise.

```
$offset = $client->getOffset(); // 2000000 bytes or 2mb
```

Delete partial upload from the cache.

```
$client->delete($key);
```

By default, the client uses `/files` as an API path. You can change it with `setApiPath` method.

```
$client->setApiPath('/api');
```

By default, the server will use `sha256` algorithm to verify the integrity of the upload. If you want to use a different hash algorithm, you can do so by using `setChecksumAlgorithm` method. To get the list of supported hash algorithms, you can send `OPTIONS` request to the server.

```
$client->setChecksumAlgorithm('crc32');
```

### Extension support

[](#extension-support)

- The Creation extension is mostly implemented and is used for creating the upload. Deferring the upload's length is not possible at the moment.
- The Termination extension is implemented which is used to terminate completed and unfinished uploads allowing the Server to free up used resources.
- The Checksum extension is implemented, the server will use `sha256` algorithm by default to verify the upload.
- The Expiration extension is implemented, details below.
- This Concatenation extension is implemented except that the server is not capable of handling unfinished concatenation.

### Expiration

[](#expiration)

The Server is capable of removing expired but unfinished uploads. You can use the following command manually or in a cron job to remove them.

```
$ ./vendor/bin/tus tus:expired --help

Usage:
  tus:expired []

Arguments:
  cache-adapter     Cache adapter to use, redis or file. Optional, defaults to file based cache.

eg:

$ ./vendor/bin/tus tus:expired redis
```

### Concatenation

[](#concatenation)

The Server is capable of concatenating multiple uploads into a single one enabling Clients to perform parallel uploads and to upload non-contiguous chunks.

```
// Actual file key
$uploadKey = uniqid();

$client->setKey($uploadKey)->file('/path/to/file', 'chunk_a.ext');

// Upload 10000 bytes starting from 1000 bytes
$bytesUploaded = $client->seek(1000)->upload(10000);
$chunkAkey     = $client->getKey();

// Upload 1000 bytes starting from 0 bytes
$bytesUploaded = $client->setFileName('chunk_b.ext')->seek(0)->upload(1000);
$chunkBkey     = $client->getKey();

// Upload remaining bytes starting from 11000 bytes (10000 +  1000)
$bytesUploaded = $client->setFileName('chunk_c.ext')->seek(11000)->upload();
$chunkCkey     = $client->getKey();

// Concatenate partial uploads
$client->setFileName('actual_file.ext')->concat($uploadKey, $chunkAkey, $chunkBkey, $chunkCkey);
```

Additionally, the server will verify checksum against the merged file to make sure that the file is not corrupt.

### Middleware

[](#middleware)

You can manipulate request and response of a server using a middleware. Middleware can be used to run a piece of code before a server calls the actual handle method. You can use middleware to authenticate a request, handle CORS, whitelist/blacklist an IP etc.

#### Creating a Middleware

[](#creating-a-middleware)

In order to create a middleware, you need to implement `TusMiddleware` interface. The handle method provides request and response object for you to manipulate.

```
