{"id":17491927,"url":"https://github.com/tcb13/thunder-tus-php","last_synced_at":"2025-04-22T20:15:18.982Z","repository":{"id":57083946,"uuid":"162048756","full_name":"TCB13/thunder-tus-php","owner":"TCB13","description":"PHP implementation of the TUS resumable upload protocol v1.0.0.","archived":false,"fork":false,"pushed_at":"2019-11-11T11:43:35.000Z","size":3280,"stargazers_count":12,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-22T20:15:12.191Z","etag":null,"topics":["file-transfer","high-concurrency","large-files","php","resumable-upload","tus","tus-protocol"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TCB13.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-16T22:58:26.000Z","updated_at":"2024-12-06T20:22:03.000Z","dependencies_parsed_at":"2022-08-24T14:56:42.955Z","dependency_job_id":null,"html_url":"https://github.com/TCB13/thunder-tus-php","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TCB13%2Fthunder-tus-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TCB13%2Fthunder-tus-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TCB13%2Fthunder-tus-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TCB13%2Fthunder-tus-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TCB13","download_url":"https://codeload.github.com/TCB13/thunder-tus-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250316066,"owners_count":21410476,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["file-transfer","high-concurrency","large-files","php","resumable-upload","tus","tus-protocol"],"created_at":"2024-10-19T08:06:15.032Z","updated_at":"2025-04-22T20:15:18.953Z","avatar_url":"https://github.com/TCB13.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ThunderTUS PHP\n\nResumable file upload in PHP using tus resumable upload protocol v1.0.0.\n\n**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.\n\n**thunder tus** is the most reliable implementation of the tus protocol for PHP yet. Designed for **high concurrency** (real world scenarios) and integration simplicity it's **free of external dependencies** (complex caching engines etc.). It is also **PSR-7 compliant** in order to bring the tus protocol to modern PHP frameworks such as **Slim 3**.\n\n**extensions**: building on the extensibility capabilities of the tus protocol, thunder tus also includes two new extensions:\n\n- **CrossCheck**: final checksum of the uploaded files to ensure maximum reliability;\n- **Express**: tus uploads with a single HTTP call - making tus better suited for mobile contexts and other environments where performance is a priority.\n\n## Installation\n\nPull the package via composer.\n```shell\n$ composer require TCB13/thunder-tus-php\n```\n\n## Basic Usage\n\nUse composer to install `tcb13/thunder-tus-php` and some other packages used in the following examples:\n```shell\n$ composer require tcb13/thunder-tus-php psr/http-message zendframework/zend-diactoros zendframework/zend-httphandlerrunner\n```\nCreate your `tus-server.php` file:\n````php\n\u003c?php\ninclude \"vendor/autoload.php\";\n\n$request = Zend\\Diactoros\\ServerRequestFactory::fromGlobals();\n$response = new Zend\\Diactoros\\Response();\n\n$backend = new FileSystem(__DIR__ . DIRECTORY_SEPARATOR . \"uploads\");\n$server = new ThunderTUS\\Server($request, $response);\n$server-\u003esetStorageBackend($backend);\n$server-\u003esetApiPath(\"/\");\n$server-\u003ehandle();\n$response = $server-\u003egetResponse();\n\n$emitter = new Zend\\HttpHandlerRunner\\Emitter\\SapiEmitter();\n$emitter-\u003eemit($response);\n````\nCreate the following `.htaccess` (or equivalent) at your virtual host:\n````\nRewriteEngine on\nRewriteBase /\nRewriteRule ^(.*)$ tus-server.php [L,QSA]\n````\nNow you can go ahead and upload a file using the TUS client included at `examples/client-express.php`.\nAfter the upload is finished you may retrieve the file in another script by calling:\n````php\n$finalStorageDirectory = \"/var/www/html/uploads\";\n$server = new ThunderTUS\\Server();\n$status = $server-\u003ecompleteAndFetch($filename, $finalStorageDirectory);\nif (!$status) {\n      throw new \\Exception(\"Could not fetch ({$filename}) from storage backend: not found.\");\n}\n````\nThe file will be moved from the temporary storage backend to the `$finalStorageDirectory` directory.\n\nYou may also retrieve the final file as a stream with `ThunderTUS\\Server::completeAndStream()` or keep on the same place as the temporary parts with `ThunderTUS\\Server::complete()`\n\n## Storage Backends\n\nIn order to use **ThunderTUS you must pick a storage backend**. Those are used to temporally store the uploaded parts until the upload is completed. Storage backends come in a variety of flavours from the local filesystem to MongoBD's GridFS:\n\n- `FileSystem`: a quick to use and understand backend for simple projects that will append uploaded parts into a file stored at the path provided on it's constructor;\n- `Amazon S3`: useful in distributed scenarios (eg. your backend serves requests from multiple machines behind a load balancer), an implementation of Amazon's S3 protocol. Tested compatibility with DigitalOcean's Spaces;\n- `Redis`:  also for distributed scenarios, stores uploaded parts into a Redis database;\n- `MongoDB`: also for distributed scenarios, will store uploaded parts inside a MongoDB GridFS bucket.\n\nYou may also implement your own storage backend by extending the `StorageBackend` class and/or implementing the `StorageInterface` interface.\n\n### S3 Storage Backend\n````php\n$server  = new \\ThunderTUS\\Server($request, $response);\n\n$client = new S3Client([\n    \"version\"     =\u003e \"latest\",\n    \"region\"      =\u003e \"...\",\n    \"endpoint\"    =\u003e \"...\",\n    \"credentials\" =\u003e [\n        \"key\"    =\u003e \"--key--\",\n        \"secret\" =\u003e \"--secret---\",\n    ],\n]);\n$backend  = new S3($client, \"your-bucket\", \"optional-path-prefix\");\n$server-\u003esetStorageBackend($backend);\n\n$server-\u003esetUploadMaxFileSize(50000);\n$server-\u003esetApiPath(\"/tus\");\n$server-\u003ehandle();\n`````\nYou may later retrieve the finished upload as described above at the basic usage section.\n\n### MongoDB Storage Backend\n````php\n// Connect to your MongDB\n$con = new \\MongoDB\\Client($configs-\u003euri, $configs-\u003eoptions]);\n$mongodb= $con-\u003eselectDatabase($configs-\u003edb]);\n\n// Start ThunderTUS\n$server  = new \\ThunderTUS\\Server($request, $response);\n\n// Load the MongoDB backend\n$mongoBackend = new MongoDB($mongodb);\n$server-\u003esetStorageBackend($mongoBackend );\n\n// Set other settings and process requests\n$server-\u003esetUploadMaxFileSize(50000);\n$server-\u003esetApiPath(\"/tus\");\n$server-\u003ehandle();\n\n// Send the response back to the client\n$response = $server-\u003egetResponse();\n````\nYou may later retrieve the finished upload as described above at the basic usage section.\n\n### Redis Storage Backend\n````php\n$server  = new \\ThunderTUS\\Server($request, $response);\n\n$redisBackend = new Redis($redisClient);\n$server-\u003esetStorageBackend($redisBackend);\n\n$server-\u003esetUploadMaxFileSize(50000);\n$server-\u003esetApiPath(\"/tus\");\n$server-\u003ehandle();\n`````\nYou may later retrieve the finished upload as described above at the basic usage section.\n\n## ThunderTUS \u0026 Dependency Injection\n\nThunderTUS was designed to be integrated into dependency injection systems / containers. \nIn simple scenarios you should pass an implementation of a PSR HTTP request and response to ThunderTUS's constructor, however this is optional. Sometimes it might be desirable to be able to instantiate the `Server` in a Service Provider and provide the PSR HTTP implementations later in a controller.\n\nExample of a **ThunderTUS service provider**:\n\n````php\npublic static function register()\n{\n    $settings = $this-\u003econtainer-\u003eget(\"settings.tusProtocol\");\n\n    // Create the server\n    $server = new Server(); // No request or response implementations passed here\n\n    // Load the filesystem Backend\n    $backend = new FileSystem($settings-\u003epath]);\n    $server-\u003esetStorageBackend($backend);\n\n    // Set TUS upload parameters\n    $server-\u003esetUploadMaxFileSize((int)$settings-\u003emaxSize);\n    $server-\u003esetApiPath($settings-\u003eendpoint);\n    \n    return $server;\n}\n````\nNow the **controller that handles uploads**:\n\n````php\npublic function upload()\n{\n    // Resolve TUS using the container\n    /** @var \\ThunderTUS\\Server $server */\n    $server = $this-\u003econtainer-\u003eget(\\ThunderTUS\\Server::class);\n\n    // Load the request or response implementations here!\n    $server-\u003eloadHTTPInterfaces($this-\u003erequest, $this-\u003eresponse); \n\n    // Handle the upload request\n    $server-\u003ehandle();\n\n    // Send the response back to the client\n    return $server-\u003egetResponse();\n}\n````\nWe've only provided the PSR HTTP request and response implementations on the controller by calling `$server-\u003eloadHTTPInterfaces(..)`.\n\n## Client Implementations\n\n- **PHP Client**: At the `examples` directory you may find a simple client and tus-crosscheck / tus-express examples as well;\n- **JavaScript / ES6**: https://github.com/stenas/thunder-tus-js-client - a very well designed and implemented tus-crosscheck / tus-express capable client with minimal footprint.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftcb13%2Fthunder-tus-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftcb13%2Fthunder-tus-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftcb13%2Fthunder-tus-php/lists"}