Skip to content

added UploadedFile::getClientPath() #7538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions system/HTTP/Files/FileCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ protected function createFileObject(array $array)
return new UploadedFile(
$array['tmp_name'] ?? null,
$array['name'] ?? null,
$array['full_path'] ?? null,
$array['type'] ?? null,
$array['size'] ?? null,
$array['error'] ?? null
Expand Down
22 changes: 21 additions & 1 deletion system/HTTP/Files/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ class UploadedFile extends File implements UploadedFileInterface
*/
protected $originalName;

/**
* The webkit relative path of the file as provided by the client for directory uploads.
*
* @var string
*/
protected $clientPath;

/**
* The filename given to a file during a move.
*
Expand Down Expand Up @@ -75,15 +82,17 @@ class UploadedFile extends File implements UploadedFileInterface
*
* @param string $path The temporary location of the uploaded file.
* @param string $originalName The client-provided filename.
* @param string $clientPath The webkit relative path as provided by the client for directory uploads.
* @param string $mimeType The type of file as provided by PHP
* @param int $size The size of the file, in bytes
* @param int $error The error constant of the upload (one of PHP's UPLOADERRXXX constants)
*/
public function __construct(string $path, string $originalName, ?string $mimeType = null, ?int $size = null, ?int $error = null)
public function __construct(string $path, string $originalName, ?string $clientPath = null, ?string $mimeType = null, ?int $size = null, ?int $error = null)
{
$this->path = $path;
$this->name = $originalName;
$this->originalName = $originalName;
$this->clientPath = $clientPath;
$this->originalMimeType = $mimeType;
$this->size = $size;
$this->error = $error;
Expand Down Expand Up @@ -267,6 +276,17 @@ public function getClientName(): string
return $this->originalName;
}

/**
* PHP 8.1+
* Gets the webkit relative path of the file as provided by the client during directory upload.
*
* @return string The webkit relative path of the file as provided by the client during directory upload, or none if PHP version is below 8.1
*/
public function getClientPath(): string
{
return $this->clientPath;
}

/**
* Gets the temporary filename where the file was uploaded to.
*/
Expand Down
10 changes: 9 additions & 1 deletion system/HTTP/Files/UploadedFileInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ interface UploadedFileInterface
*
* @param string $path The temporary location of the uploaded file.
* @param string $originalName The client-provided filename.
* @param string $webkitPath The webkit relative path as provided by the client.
* @param string $mimeType The type of file as provided by PHP
* @param int $size The size of the file, in bytes
* @param int $error The error constant of the upload (one of PHP's UPLOADERRXXX constants)
*/
public function __construct(string $path, string $originalName, ?string $mimeType = null, ?int $size = null, ?int $error = null);
public function __construct(string $path, string $originalName, ?string $webkitPath = null, ?string $mimeType = null, ?int $size = null, ?int $error = null);

/**
* Move the uploaded file to a new location.
Expand Down Expand Up @@ -109,6 +110,13 @@ public function getName(): string;
*/
public function getTempName(): string;

/**
* Returns the webkit relative path of the file as provided by the client during directory upload.
*
* @return string The webkit relative path of the file as provided by the client during directory upload, or null if PHP version is below 8.1
*/
public function getClientPath(): string | null;

/**
* Returns the original file extension, based on the file name that
* was uploaded. This is NOT a trusted source.
Expand Down
2 changes: 2 additions & 0 deletions tests/system/HTTP/Files/FileCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testAllReturnsValidSingleFile()
'type' => 'text/plain',
'size' => '124',
'tmp_name' => '/tmp/myTempFile.txt',
'full_path' => '/tmp/myTempFile.txt',
'error' => 0,
],
];
Expand All @@ -54,6 +55,7 @@ public function testAllReturnsValidSingleFile()
$this->assertInstanceOf(UploadedFile::class, $file);

$this->assertSame('someFile.txt', $file->getName());
$this->assertSame('/tmp/myTempFile.txt', $file->getClientPath());
$this->assertSame(124, $file->getSize());
}

Expand Down