Files

56 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2026-11-17 16:15:01 -05:00
<?php
/**
* Uploads files in 5mb chunk.
*/
ini_set('display_errors', 1);
ini_set("log_errors", 1);
ini_set("error_log", "/home/httpd/html/mojo.bateworld.com/public_html/tus-php/errors.txt");
error_log( "Hello, errors!" );
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "aaa\n";
fwrite($myfile, $txt);
require __DIR__ . '/../../vendor/autoload.php';
use TusPhp\Exception\TusException;
use TusPhp\Exception\FileException;
use TusPhp\Exception\ConnectionException;
$client = new \TusPhp\Tus\Client('http://tus-php-server');
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "ccc\n";
fwrite($myfile, $txt);
// Alert: Sanitize all inputs properly in production code
if ( ! empty($_FILES)) {
$fileMeta = $_FILES['tus_file'];
$uploadKey = hash_file('md5', $fileMeta['tmp_name']);
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "bbb\n";
fwrite($myfile, $txt);
try {
$client->setKey($uploadKey)->file("/home/httpd/html/mojo.bateworld.com/public_html/tus-php/example/", time() . '_' . $fileMeta['name']);
$bytesUploaded = $client->upload(5000000); // Chunk of 5 mb
$client->file("/home/httpd/html/mojo.bateworld.com/public_html/tus-php/example/", $fileMeta['name'])->upload();
echo json_encode([
'status' => 'uploading',
'bytes_uploaded' => $bytesUploaded,
'upload_key' => $uploadKey
]);
} catch (ConnectionException | FileException | TusException $e) {
echo json_encode([
'status' => 'error',
'bytes_uploaded' => -1,
'upload_key' => '',
'error' => $e->getMessage(),
]);
}
} else {
echo json_encode([
'status' => 'error',
'bytes_uploaded' => -1,
'error' => 'No input!',
]);
}