2016-01-06 -> 2021-03-05
Helps implementing a server side service to handle file uploads.
Uploader is part of the universe framework.
Using the planet installer via light-cli
lt install Ling.Uploader
Using the uni command.
uni import Ling/Uploader
There is an Uploader object, with an upload method:
array|false upload ( mixed:file )
This method returns either an array of paths where the files have been uploaded, or false in case of errors. There is also a getErrors method for retrieving errors.
An uploader object SHOULD take care of:
- validating the input file (correct size, type, ...)
- processing the file (choose the destination path, create multiple thumbnails)
Here is a server implementation using Tim 1.1.0. You can use this server with dropzone.js for instance.
<?php
//------------------------------------------------------------------------------/
// EXAMPLE SERVER FOR FILE UPLOADING
//------------------------------------------------------------------------------/
use Ling\Tim\TimServer\OpaqueTimServer;
use Ling\Tim\TimServer\TimServerInterface;
use Ling\Uploader\Exception\UploaderUserException;
require_once __DIR__ . "/../../init.php"; // application general init
/**
* The upload.init.php file defines the getUploader function,
* which returns either false, or a valid UploaderInterface object.
*/
require_once __DIR__ . "/../../config/upload.init.php";
OpaqueTimServer::create()->start(function (TimServerInterface $server) {
try {
if (
isset($_FILES['file']) &&
isset($_GET['uploadId'])
) {
$file = $_FILES['file'];
$uploadId = $_GET['uploadId'];
if (false !== $uploader = getUploader($uploadId)) {
if (false !== $files = $uploader->upload($file)) {
// $files are uploaded
$server->success($files);
}
else {
$s = '';
foreach ($uploader->getErrors() as $msg) {
$s .= $msg . PHP_EOL;
}
$server->error($s);
}
}
else {
$server->error("Invalid uploadId $uploadId");
}
}
else {
$server->error("invalid params");
}
} catch (UploaderUserException $e) {
$server->error($e->getMessage());
}
})->output();
And the upload.init.php file could contain something like this:
<?php
use Ling\Uploader\Exception\UploaderUserException;
use Ling\Uploader\File\PhpFile;
use Ling\Uploader\Uploader\PhpFileUploader;
use Ling\Uploader\Uploader\UploaderInterface;
/**
* @param $uploaderId
* @return UploaderInterface
*/
function getUploader($uploaderId)
{
switch ($uploaderId) {
case 'thumbnail-table1':
return PhpFileUploader::create()
->setProcessFileCb(function (PhpFile $f, array &$files) {
$fileName = $f->name;
// upload a thumbnail somewhere...
$target = '/tmp/' . $fileName;
$res = move_uploaded_file($f->tmp_name, $target);
if (false === $res) {
throw new UploaderUserException("The file couldn't be uploaded");
}
$files[] = $target;
}
);
break;
}
return false;
}
-
1.0.4 -- 2021-05-31
- Removing trailing plus in lpi-deps file (to work with Light_PlanetInstaller:2.0.0 api
-
1.0.3 -- 2021-03-05
- update README.md, add install alternative
-
1.0.2 -- 2020-12-08
- Fix lpi-deps not using natsort.
-
1.0.1 -- 2020-12-04
- Add lpi-deps.byml file
-
1.0.0 -- 2016-01-06
- initial commit