PHP code example of district5 / slim-psr-upload-handler
1. Go to this page and download the library: Download district5/slim-psr-upload-handler library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
district5 / slim-psr-upload-handler example snippets
use \District5\UploadHandler\UploadHandler;
use \Slim\Psr7\Request;
use \Slim\Psr7\Response;
$container = new \Slim\Container();
// ... add any other dependencies to the container before or after the upload handler
$uploadHandlerConfig = [
'options' => [ // these are the core library options. these are the defaults for all handlers, but can be overridden in the handler specific config
'suppressExceptions' => false, // ignore errors and return the UploadedDto object regardless.
'appendRandom' => true, // append a random string (using uniqid) to the file, or use the original name
],
'handlers' => [
'example-google-cloud-storage-provider' => [ // 'my-gcs-provider' is the action name, this can be anything you want
'provider' => 'gcs', // Or use the class name of GcsProvider::class
'config' => [
'suppressExceptions' => false, // Override the global suppressExceptions option
'appendRandom' => false, // append a random string (using uniqid) to the file, or use the original name. Overrides the global appendRandom option
'overwrite' => false, // overwrite the file if it already exists
'projectId' => $env->get('GCS_PROJECT_ID'), // your GCP project id
'bucket' => $env->get('GCS_BUCKET'), // your GCS bucket name
'path' => $env->get('GCS_OBJECT_PATH'), // or null/empty string for root
'keyFile' => json_decode($env->get('GCS_KEY_FILE_CONTENT'), true), // your GCP service account key file content,
'acl' => $env->get('GCS_OBJECT_ACL'), // the GCS object ACL
]
],
'example-aws-s3-provider' => [ // 'generic-file' is the action name, this can be anything you want
'provider' => 's3',
'config' => [
'suppressExceptions' => false, // suppress exceptions, or let them bubble up
'appendRandom' => true, // append a random string (using uniqid) to the file, or use the original name. Overrides the global appendRandom option
'overwrite' => false, // overwrite the file if it already exists
'region' => $env->get('S3_REGION'), // the region of the bucket
'version' => $env->get('S3_VERSION'), // the version of the S3 API to use (typically 'latest')
'bucket' => $env->get('S3_BUCKET'), // the name of the bucket
'path' => $env->get('S3_OBJECT_PATH'), // or null/empty string for root
'accessKey' => $env->get('S3_ACCESS_KEY'), // the access key for the bucket
'secretKey' => $env->get('S3_SECRET_KEY'), // the secret key for the bucket
'acl' => $env->get('S3_OBJECT_ACL') // the acl for the object (public-read, private, etc.)
]
],
'example-local-file-provider' => [ // 'generic-file' is the action name, this can be anything you want
'provider' => 'local', // Or use the class name of LocalFileProvider::class
'config' => [
// 'suppressExceptions' => false, // Override the global suppressExceptions option
// 'appendRandom' => false, // append a random string (using uniqid) to the file, or use the original name. Overrides the global appendRandom option
'overwrite' => false, // overwrite the file if it already exists
'path' => $env->get('LOCAL_WRITABLE_DIRECTORY'), // the directory to save the file to (trailing slash is stripped)
]
]
]
];
UploadHandler::create($container, $uploadHandlerConfig);
$app = new \Slim\App($container);
$app->post('/upload', function (Request $request, Response $response, $args) {
$file = $request->getUploadedFiles()['param-key'];
/* @var $container \DI\Container */
$uploadHandler = $container->get(UploadHandler::class);
/* @var $uploadHandler UploadHandler */
$result = $uploadHandler->handleFromUpload('example-google-cloud-storage-provider', $file);
// or: $result = $uploadHandler->handleFromUpload('example-local-file-provider', $filePath);
return $response->withJson($result);
});
use Slim\Psr7\UploadedFile;
use District5\UploadHandler\Providers\ProviderAbstract;
use District5\UploadHandler\UploadedDto;
/**
* Class MyFileProvider
*/
class MyFileProvider extends ProviderAbstract
{
/**
* @param UploadedFile $file
* @return UploadedDto
*/
protected function processFileFromUpload(UploadedFile $file): UploadedDto
{
try {
$fileName = $file->getClientFilename();
$newFileName = $this->getFileName($fileName); // this handles the appending of a random string if ble $e) {
if ($this->suppressException()) {
return UploadedDto::createError($this, $e);
}
throw $e; // re-throw the exception
}
}
/**
* @param string $filePath
* @return UploadedDto
*/
protected function processFileFromLocal(string $filePath): UploadedDto
{
try {
$fileName = basename($filePath);
$newFileName = $this->getFileName($fileName);
$localDirectory = $this->getConfig('path');
$localPath = rtrim($localDirectory, DIRECTORY_SEPARATOR) . '/' . $newFileName;
copy($filePath, $localPath);
$mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $filePath);
$size = filesize($filePath);
return new UploadedDto(
$this,
null,
$localPath,
$baseName,
$newFileName,
$mime,
$size,
pathinfo($localPath),
true
);
} catch (Throwable $e) {
if ($this->suppressException()) {
return UploadedDto::createError($this, $e);
}
throw $e; // re-throw the exception
}
}
/**
* Get an array of
$uploadHandlerConfig = [
'handlers' => [
'my-file-provider' => [ // 'my-file-provider' is the action name, this can be anything you want
'provider' => MyFileProvider::class, // the class name of your provider
'config' => [ // the configuration for your provider
'path' => '/tmp'
]
]
]
];
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.