Skip to content

Commit 5ea0f6f

Browse files
authored
Merge pull request #7541 from JamminCoder/uploaded-file-full_path
Add access to `full_path` index of uploaded files
2 parents 2ba179c + 0b59654 commit 5ea0f6f

File tree

7 files changed

+81
-3
lines changed

7 files changed

+81
-3
lines changed

system/HTTP/Files/FileCollection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ protected function createFileObject(array $array)
182182
$array['name'] ?? null,
183183
$array['type'] ?? null,
184184
$array['size'] ?? null,
185-
$array['error'] ?? null
185+
$array['error'] ?? null,
186+
$array['full_path'] ?? null
186187
);
187188
}
188189

system/HTTP/Files/UploadedFile.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ class UploadedFile extends File implements UploadedFileInterface
3434
*/
3535
protected $path;
3636

37+
/**
38+
* The webkit relative path of the file.
39+
*
40+
* @var string
41+
*/
42+
protected $clientPath;
43+
3744
/**
3845
* The original filename as provided by the client.
3946
*
@@ -78,15 +85,17 @@ class UploadedFile extends File implements UploadedFileInterface
7885
* @param string $mimeType The type of file as provided by PHP
7986
* @param int $size The size of the file, in bytes
8087
* @param int $error The error constant of the upload (one of PHP's UPLOADERRXXX constants)
88+
* @param string $clientPath The webkit relative path of the uploaded file.
8189
*/
82-
public function __construct(string $path, string $originalName, ?string $mimeType = null, ?int $size = null, ?int $error = null)
90+
public function __construct(string $path, string $originalName, ?string $mimeType = null, ?int $size = null, ?int $error = null, ?string $clientPath = null)
8391
{
8492
$this->path = $path;
8593
$this->name = $originalName;
8694
$this->originalName = $originalName;
8795
$this->originalMimeType = $mimeType;
8896
$this->size = $size;
8997
$this->error = $error;
98+
$this->clientPath = $clientPath;
9099

91100
parent::__construct($path, false);
92101
}
@@ -267,6 +276,15 @@ public function getClientName(): string
267276
return $this->originalName;
268277
}
269278

279+
/**
280+
* (PHP 8.1+)
281+
* Returns the webkit relative path of the uploaded file on directory uploads.
282+
*/
283+
public function getClientPath(): ?string
284+
{
285+
return $this->clientPath;
286+
}
287+
270288
/**
271289
* Gets the temporary filename where the file was uploaded to.
272290
*/

system/HTTP/Files/UploadedFileInterface.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ interface UploadedFileInterface
3131
* @param string $mimeType The type of file as provided by PHP
3232
* @param int $size The size of the file, in bytes
3333
* @param int $error The error constant of the upload (one of PHP's UPLOADERRXXX constants)
34+
* @param string $clientPath The webkit relative path of the uploaded file.
3435
*/
35-
public function __construct(string $path, string $originalName, ?string $mimeType = null, ?int $size = null, ?int $error = null);
36+
public function __construct(string $path, string $originalName, ?string $mimeType = null, ?int $size = null, ?int $error = null, ?string $clientPath = null);
3637

3738
/**
3839
* Move the uploaded file to a new location.
@@ -109,6 +110,12 @@ public function getName(): string;
109110
*/
110111
public function getTempName(): string;
111112

113+
/**
114+
* (PHP 8.1+)
115+
* Returns the webkit relative path of the uploaded file on directory uploads.
116+
*/
117+
public function getClientPath(): ?string;
118+
112119
/**
113120
* Returns the original file extension, based on the file name that
114121
* was uploaded. This is NOT a trusted source.

tests/system/HTTP/Files/FileCollectionTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,41 @@ public function testErrorWithNoError()
453453
$this->assertSame(UPLOAD_ERR_OK, $file->getError());
454454
}
455455

456+
public function testClientPathReturnsValidFullPath()
457+
{
458+
$_FILES = [
459+
'userfile' => [
460+
'name' => 'someFile.txt',
461+
'type' => 'text/plain',
462+
'size' => '124',
463+
'tmp_name' => '/tmp/myTempFile.txt',
464+
'full_path' => 'someDir/someFile.txt',
465+
],
466+
];
467+
468+
$collection = new FileCollection();
469+
$file = $collection->getFile('userfile');
470+
471+
$this->assertSame('someDir/someFile.txt', $file->getClientPath());
472+
}
473+
474+
public function testClientPathReturnsNullWhenFullPathIsNull()
475+
{
476+
$_FILES = [
477+
'userfile' => [
478+
'name' => 'someFile.txt',
479+
'type' => 'text/plain',
480+
'size' => '124',
481+
'tmp_name' => '/tmp/myTempFile.txt',
482+
],
483+
];
484+
485+
$collection = new FileCollection();
486+
$file = $collection->getFile('userfile');
487+
488+
$this->assertNull($file->getClientPath());
489+
}
490+
456491
public function testFileReturnsValidSingleFile()
457492
{
458493
$_FILES = [

user_guide_src/source/changelogs/v4.4.0.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ Libraries
8989
the actual validated data. See :ref:`validation-getting-validated-data` for details.
9090
- **Images:** The option ``$quality`` can now be used to compress WebP images.
9191

92+
- **Uploaded Files:** Added ``UploadedFiles::getClientPath()`` method that returns
93+
the value of the `full_path` index of the file if it was uploaded via directory upload.
94+
9295
Helpers and Functions
9396
=====================
9497

user_guide_src/source/libraries/uploaded_files.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,16 @@ version, use ``getMimeType()`` instead:
304304

305305
.. literalinclude:: uploaded_files/015.php
306306

307+
getClientPath()
308+
---------------
309+
310+
.. versionadded:: 4.4.0
311+
312+
Returns the `webkit relative path <https://developer.mozilla.org/en-US/docs/Web/API/File/webkitRelativePath>`_ of the uploaded file when the client has uploaded files via directory upload.
313+
In PHP versions below 8.1, this returns ``null``
314+
315+
.. literalinclude:: uploaded_files/023.php
316+
307317
Moving Files
308318
============
309319

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
$clientPath = $file->getClientPath();
4+
echo $clientPath; // dir/file.txt, or dir/sub_dir/file.txt

0 commit comments

Comments
 (0)