Comments (5)
@terrylinooo uppy internally uses tus-js-client. When using tus-php with uppy you don't need to do anything but pass the tus endpoint. The step would be something like this:
- Setup tus server using tus-php
- Pass server url to
endpoint
in uppy config as you did
Client part is auto handled by uppy. You can refer to this uppy integration example.
How to setup server is defined here. If you are using Laravel/Lumen framework, you can refer this doc.
from tus-php.
Thank you. I finally give up to try uppy.
I use your basic example in Codeigniter 3 framework and it works good. Thank you.
from tus-php.
Here is my code in Codeigniter 3, if someone need it.
<?php
use TusPhp\Exception\FileException;
use TusPhp\Exception\ConnectionException;
use TusPhp\Exception\Exception as TusException;
class TusServer extends MY_Controller
{
public $fileStoreFolder = '';
public $tusServerUrl = '';
public $tusServerPath = '';
public function __construct()
{
parent::__construct();
$this->fileStoreFolder = APPPATH . '../resource/upload';
$this->tusServerUrl = base_url() . 'tusServer/server';
$this->tusServerPath = 'tusServer/server';
$oldUmask = umask(0);
if (!is_dir($this->fileStoreFolder)) {
mkdir($this->fileStoreFolder, 0777, true);
}
umask($oldUmask);
}
/**
* Tus Server
*/
public function server()
{
$server = new \TusPhp\Tus\Server('redis');
$server->setMaxUploadSize(2000000);
$server->setApiPath($this->tusServerPath);
$server->setUploadDir($this->fileStoreFolder);
$response = $server->serve();
$response->send();
exit(0);
}
/**
* Upload
*/
public function upload()
{
$client = new \TusPhp\Tus\Client(base_url(), 'redis');
$client->setApiPath($this->tusServerPath);
if ( ! empty($_FILES)) {
$fileMeta = $_FILES['tus_file'];
$uploadKey = hash_file('md5', $fileMeta['tmp_name']);
try {
$client->setKey($uploadKey)->file($fileMeta['tmp_name'], time() . '_' . $fileMeta['name']);
$bytesUploaded = $client->upload(2000000); // Chunk of 5 mb
echo json_encode([
'status' => 'uploading',
'bytes_uploaded' => $bytesUploaded,
'checksum' => $client->getChecksum(),
]);
} catch (ConnectionException | FileException | TusException $e) {
echo json_encode([
'status' => 'error',
'bytes_uploaded' => -1,
'checksum' => '',
'error' => $e->getMessage(),
]);
}
} else {
echo json_encode([
'status' => 'error',
'bytes_uploaded' => -1,
'error' => 'No input!',
]);
}
}
/**
* Verify
*/
public function verify()
{
$client = new \TusPhp\Tus\Client(base_url(), 'redis');
$client->setApiPath($this->tusServerPath);
if (!empty($_FILES)) {
$status = 'new';
$fileMeta = $_FILES['tus_file'];
$uploadKey = hash_file('md5', $fileMeta['tmp_name']);
try {
$offset = $client->setKey($uploadKey)->file($fileMeta['tmp_name'])->getOffset();
if (false !== $offset) {
$status = $offset >= $fileMeta['size'] ? 'uploaded' : 'resume';
} else {
$offset = 0;
}
echo json_encode([
'status' => $status,
'bytes_uploaded' => $offset,
'checksum' => $client->getChecksum(),
]);
} catch (ConnectionException | ConnectException $e) {
echo json_encode([
'status' => 'error',
'bytes_uploaded' => -1,
]);
} catch (FileException $e) {
echo json_encode([
'status' => 'resume',
'bytes_uploaded' => 0,
'checksum' => '',
]);
}
} else {
echo json_encode([
'status' => 'error',
'bytes_uploaded' => -1,
]);
}
}
}
from tus-php.
Thanks for sharing 👍
from tus-php.
Here is my code in Codeigniter 3, if someone need it.
<?php use TusPhp\Exception\FileException; use TusPhp\Exception\ConnectionException; use TusPhp\Exception\Exception as TusException; class TusServer extends MY_Controller { public $fileStoreFolder = ''; public $tusServerUrl = ''; public $tusServerPath = ''; public function __construct() { parent::__construct(); $this->fileStoreFolder = APPPATH . '../resource/upload'; $this->tusServerUrl = base_url() . 'tusServer/server'; $this->tusServerPath = 'tusServer/server'; $oldUmask = umask(0); if (!is_dir($this->fileStoreFolder)) { mkdir($this->fileStoreFolder, 0777, true); } umask($oldUmask); } /** * Tus Server */ public function server() { $server = new \TusPhp\Tus\Server('redis'); $server->setMaxUploadSize(2000000); $server->setApiPath($this->tusServerPath); $server->setUploadDir($this->fileStoreFolder); $response = $server->serve(); $response->send(); exit(0); } /** * Upload */ public function upload() { $client = new \TusPhp\Tus\Client(base_url(), 'redis'); $client->setApiPath($this->tusServerPath); if ( ! empty($_FILES)) { $fileMeta = $_FILES['tus_file']; $uploadKey = hash_file('md5', $fileMeta['tmp_name']); try { $client->setKey($uploadKey)->file($fileMeta['tmp_name'], time() . '_' . $fileMeta['name']); $bytesUploaded = $client->upload(2000000); // Chunk of 5 mb echo json_encode([ 'status' => 'uploading', 'bytes_uploaded' => $bytesUploaded, 'checksum' => $client->getChecksum(), ]); } catch (ConnectionException | FileException | TusException $e) { echo json_encode([ 'status' => 'error', 'bytes_uploaded' => -1, 'checksum' => '', 'error' => $e->getMessage(), ]); } } else { echo json_encode([ 'status' => 'error', 'bytes_uploaded' => -1, 'error' => 'No input!', ]); } } /** * Verify */ public function verify() { $client = new \TusPhp\Tus\Client(base_url(), 'redis'); $client->setApiPath($this->tusServerPath); if (!empty($_FILES)) { $status = 'new'; $fileMeta = $_FILES['tus_file']; $uploadKey = hash_file('md5', $fileMeta['tmp_name']); try { $offset = $client->setKey($uploadKey)->file($fileMeta['tmp_name'])->getOffset(); if (false !== $offset) { $status = $offset >= $fileMeta['size'] ? 'uploaded' : 'resume'; } else { $offset = 0; } echo json_encode([ 'status' => $status, 'bytes_uploaded' => $offset, 'checksum' => $client->getChecksum(), ]); } catch (ConnectionException | ConnectException $e) { echo json_encode([ 'status' => 'error', 'bytes_uploaded' => -1, ]); } catch (FileException $e) { echo json_encode([ 'status' => 'resume', 'bytes_uploaded' => 0, 'checksum' => '', ]); } } else { echo json_encode([ 'status' => 'error', 'bytes_uploaded' => -1, ]); } } }
can you give more details of how you put library in application and share sample code with usage in controller.
from tus-php.
Related Issues (20)
- File based cache - tus_php.server.cache empty - 410 Error HOT 5
- Continue file upload from different browser and device HOT 3
- How to protect single file link? HOT 2
- CakePHP integration does not work HOT 2
- How can I do Cloudflare Integration? HOT 2
- Location header set to http when deployed behind reverse proxy HOT 2
- Unable to upload files - PATCH GONE HOT 1
- Getting Started Documentation
- Allow PHP 8+ for version 1.x.x
- Checksum implementation does not match TUS specification
- Enable to update to PHP 8.1 due to version constraint
- Documentation, Please!
- validating file mime type
- Headers can be totally configurable HOT 1
- Please kindly explain this code to me HOT 1
- Password protected Redis
- Possible bug or configuration issue? HOT 3
- Unable to create resource.
- PHP 8.1+ support HOT 3
- Laravel Octane support
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tus-php.