fipamo/brain/utility/FileUploader.php

33 lines
1 KiB
PHP
Raw Permalink Normal View History

<?php
2022-03-20 03:36:15 +01:00
namespace brain\utility;
class FileUploader
{
public static function uploadFile(string $directory, $file)
{
$response = [];
try {
if (!is_dir($directory)) {
2022-03-20 03:36:15 +01:00
// Directory does not exist, so lets create it.
mkdir($directory, 0755, true);
}
2022-03-20 03:36:15 +01:00
// $upload = move_uploaded_file($file->getClientFileName(), $directory);
// $extension = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
2022-03-20 03:36:15 +01:00
// see http://php.net/manual/en/function.random-bytes.php
// $basename = bin2hex(random_bytes(8));
// $filename = sprintf("%s.%0.8s", $basename, $extension);
2022-03-20 03:36:15 +01:00
// echo "**FILE** " . $file->getClientFileName();
$file->moveTo($directory . '/' . urlencode($file->getClientFileName()));
} catch (RuntimeException $e) {
echo 'ERROR ' . $e->getMessage();
2022-03-20 03:36:15 +01:00
// echo "failed to upload image: " . $e->getMessage();
// throw new Error("Failed to upload image file");
}
}
}