diff --git a/src/Http/FileUpload.php b/src/Http/FileUpload.php index 4afd6656..76e48f75 100644 --- a/src/Http/FileUpload.php +++ b/src/Http/FileUpload.php @@ -42,13 +42,16 @@ final class FileUpload private readonly int $error; - public function __construct(?array $value) + public function __construct(array|string|null $value) { - foreach (['name', 'size', 'tmp_name', 'error'] as $key) { - if (!isset($value[$key]) || !is_scalar($value[$key])) { - $value = []; - break; - } + if (is_string($value)) { + $value = [ + 'name' => basename($value), + 'full_path' => $value, + 'size' => filesize($value), + 'tmp_name' => $value, + 'error' => UPLOAD_ERR_OK, + ]; } $this->name = $value['name'] ?? ''; diff --git a/tests/Http/FileUpload.basic.phpt b/tests/Http/FileUpload.basic.phpt index fc01ab61..acce33f4 100644 --- a/tests/Http/FileUpload.basic.phpt +++ b/tests/Http/FileUpload.basic.phpt @@ -85,3 +85,22 @@ test('', function () { Assert::null($upload->getSuggestedExtension()); Assert::same('', (string) $upload); }); + + +test('', function () { + $upload = new FileUpload($file = __DIR__ . '/files/file.txt'); + + Assert::same('file.txt', $upload->getName()); + Assert::same('file.txt', $upload->getUntrustedName()); + Assert::same('file.txt', $upload->getSanitizedName()); + Assert::same($file, $upload->getUntrustedFullPath()); + Assert::same(filesize($file), $upload->getSize()); + Assert::same($file, $upload->getTemporaryFile()); + Assert::same($file, (string) $upload); + Assert::same(0, $upload->getError()); + Assert::true($upload->isOk()); + Assert::true($upload->hasFile()); + Assert::false($upload->isImage()); + Assert::null($upload->getSuggestedExtension()); + Assert::same(file_get_contents($file), $upload->getContents()); +});