mirror of https://github.com/flightphp/core
Merge pull request #602 from flightphp/file-upload-handler
Added ability to handle file uploads in a simple waypull/605/head v3.12.0
commit
63fbf9b031
@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace flight\net;
|
||||
|
||||
use Exception;
|
||||
|
||||
class UploadedFile
|
||||
{
|
||||
/**
|
||||
* @var string $name The name of the uploaded file.
|
||||
*/
|
||||
private string $name;
|
||||
|
||||
/**
|
||||
* @var string $mimeType The MIME type of the uploaded file.
|
||||
*/
|
||||
private string $mimeType;
|
||||
|
||||
/**
|
||||
* @var int $size The size of the uploaded file in bytes.
|
||||
*/
|
||||
private int $size;
|
||||
|
||||
/**
|
||||
* @var string $tmpName The temporary name of the uploaded file.
|
||||
*/
|
||||
private string $tmpName;
|
||||
|
||||
/**
|
||||
* @var int $error The error code associated with the uploaded file.
|
||||
*/
|
||||
private int $error;
|
||||
|
||||
/**
|
||||
* Constructs a new UploadedFile object.
|
||||
*
|
||||
* @param string $name The name of the uploaded file.
|
||||
* @param string $mimeType The MIME type of the uploaded file.
|
||||
* @param int $size The size of the uploaded file in bytes.
|
||||
* @param string $tmpName The temporary name of the uploaded file.
|
||||
* @param int $error The error code associated with the uploaded file.
|
||||
*/
|
||||
public function __construct(string $name, string $mimeType, int $size, string $tmpName, int $error)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->mimeType = $mimeType;
|
||||
$this->size = $size;
|
||||
$this->tmpName = $tmpName;
|
||||
$this->error = $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the client-side filename of the uploaded file.
|
||||
*
|
||||
* @return string The client-side filename.
|
||||
*/
|
||||
public function getClientFilename(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the media type of the uploaded file as provided by the client.
|
||||
*
|
||||
* @return string The media type of the uploaded file.
|
||||
*/
|
||||
public function getClientMediaType(): string
|
||||
{
|
||||
return $this->mimeType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size of the uploaded file.
|
||||
*
|
||||
* @return int The size of the uploaded file.
|
||||
*/
|
||||
public function getSize(): int
|
||||
{
|
||||
return $this->size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the temporary name of the uploaded file.
|
||||
*
|
||||
* @return string The temporary name of the uploaded file.
|
||||
*/
|
||||
public function getTempName(): string
|
||||
{
|
||||
return $this->tmpName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error code associated with the uploaded file.
|
||||
*
|
||||
* @return int The error code.
|
||||
*/
|
||||
public function getError(): int
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the uploaded file to the specified target path.
|
||||
*
|
||||
* @param string $targetPath The path to move the file to.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function moveTo(string $targetPath): void
|
||||
{
|
||||
if ($this->error !== UPLOAD_ERR_OK) {
|
||||
throw new Exception($this->getUploadErrorMessage($this->error));
|
||||
}
|
||||
|
||||
$isUploadedFile = is_uploaded_file($this->tmpName) === true;
|
||||
if (
|
||||
$isUploadedFile === true
|
||||
&&
|
||||
move_uploaded_file($this->tmpName, $targetPath) === false
|
||||
) {
|
||||
throw new Exception('Cannot move uploaded file'); // @codeCoverageIgnore
|
||||
} elseif ($isUploadedFile === false && getenv('PHPUNIT_TEST')) {
|
||||
rename($this->tmpName, $targetPath);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the error message for a given upload error code.
|
||||
*
|
||||
* @param int $error The upload error code.
|
||||
*
|
||||
* @return string The error message.
|
||||
*/
|
||||
protected function getUploadErrorMessage(int $error): string
|
||||
{
|
||||
switch ($error) {
|
||||
case UPLOAD_ERR_INI_SIZE:
|
||||
return 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
|
||||
case UPLOAD_ERR_FORM_SIZE:
|
||||
return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
|
||||
case UPLOAD_ERR_PARTIAL:
|
||||
return 'The uploaded file was only partially uploaded.';
|
||||
case UPLOAD_ERR_NO_FILE:
|
||||
return 'No file was uploaded.';
|
||||
case UPLOAD_ERR_NO_TMP_DIR:
|
||||
return 'Missing a temporary folder.';
|
||||
case UPLOAD_ERR_CANT_WRITE:
|
||||
return 'Failed to write file to disk.';
|
||||
case UPLOAD_ERR_EXTENSION:
|
||||
return 'A PHP extension stopped the file upload.';
|
||||
default:
|
||||
return 'An unknown error occurred. Error code: ' . $error;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace tests;
|
||||
|
||||
use Exception;
|
||||
use flight\net\UploadedFile;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class UploadedFileTest extends TestCase
|
||||
{
|
||||
public function tearDown(): void
|
||||
{
|
||||
if (file_exists('file.txt')) {
|
||||
unlink('file.txt');
|
||||
}
|
||||
if (file_exists('tmp_name')) {
|
||||
unlink('tmp_name');
|
||||
}
|
||||
}
|
||||
|
||||
public function testMoveToSuccess()
|
||||
{
|
||||
file_put_contents('tmp_name', 'test');
|
||||
$uploadedFile = new UploadedFile('file.txt', 'text/plain', 4, 'tmp_name', UPLOAD_ERR_OK);
|
||||
$uploadedFile->moveTo('file.txt');
|
||||
$this->assertFileExists('file.txt');
|
||||
}
|
||||
|
||||
public function getFileErrorMessageTests(): array
|
||||
{
|
||||
return [
|
||||
[ UPLOAD_ERR_INI_SIZE, 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', ],
|
||||
[ UPLOAD_ERR_FORM_SIZE, 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', ],
|
||||
[ UPLOAD_ERR_PARTIAL, 'The uploaded file was only partially uploaded.', ],
|
||||
[ UPLOAD_ERR_NO_FILE, 'No file was uploaded.', ],
|
||||
[ UPLOAD_ERR_NO_TMP_DIR, 'Missing a temporary folder.', ],
|
||||
[ UPLOAD_ERR_CANT_WRITE, 'Failed to write file to disk.', ],
|
||||
[ UPLOAD_ERR_EXTENSION, 'A PHP extension stopped the file upload.', ],
|
||||
[ -1, 'An unknown error occurred. Error code: -1' ]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getFileErrorMessageTests
|
||||
*/
|
||||
public function testMoveToFailureMessages($error, $message)
|
||||
{
|
||||
file_put_contents('tmp_name', 'test');
|
||||
$uploadedFile = new UploadedFile('file.txt', 'text/plain', 4, 'tmp_name', $error);
|
||||
$this->expectException(Exception::class);
|
||||
$this->expectExceptionMessage($message);
|
||||
$uploadedFile->moveTo('file.txt');
|
||||
}
|
||||
}
|
Loading…
Reference in new issue