2021-04-14 00:20:57 +02:00
|
|
|
<?php
|
|
|
|
use Psr\Http\Message\UploadedFileInterface;
|
|
|
|
//include "brain/data/Auth.inc.php";
|
2021-04-24 05:54:56 +02:00
|
|
|
define("MAXIMUM_FILESIZE", "10485760"); //10 MB
|
2021-04-14 00:20:57 +02:00
|
|
|
class FileUploader
|
|
|
|
{
|
2021-04-24 05:54:56 +02:00
|
|
|
public static function uploadFile(string $directory, $file)
|
|
|
|
{
|
2021-04-17 01:15:47 +02:00
|
|
|
$response = [];
|
2021-04-14 00:20:57 +02:00
|
|
|
try {
|
|
|
|
if (!is_dir($directory)) {
|
|
|
|
//Directory does not exist, so lets create it.
|
|
|
|
mkdir($directory, 0755, true);
|
|
|
|
}
|
|
|
|
//$upload = move_uploaded_file($file->getClientFileName(), $directory);
|
|
|
|
//$extension = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
|
|
|
|
|
|
|
|
// see http://php.net/manual/en/function.random-bytes.php
|
|
|
|
//$basename = bin2hex(random_bytes(8));
|
|
|
|
//$filename = sprintf("%s.%0.8s", $basename, $extension);
|
|
|
|
|
2021-04-24 05:54:56 +02:00
|
|
|
//echo "**FILE** " . $file->getClientFileName();
|
|
|
|
|
2022-02-24 00:48:11 +01:00
|
|
|
$file->moveTo($directory . "/" . urlencode($file->getClientFileName()));
|
2021-04-24 05:54:56 +02:00
|
|
|
} catch (RuntimeException $e) {
|
|
|
|
echo "ERROR " . $e->getMessage();
|
2021-04-17 01:15:47 +02:00
|
|
|
|
|
|
|
//echo "failed to upload image: " . $e->getMessage();
|
|
|
|
//throw new Error("Failed to upload image file");
|
2021-04-14 00:20:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|