Skip to content
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

Add access to full_path index of uploaded files #7541

Merged
merged 13 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 19 additions & 1 deletion system/HTTP/Files/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class UploadedFile extends File implements UploadedFileInterface
*/
protected $path;

/**
* The webkit relative path of the file.
*
* @var string
*/
protected $clientPath;

/**
* The original filename as provided by the client.
*
Expand Down Expand Up @@ -75,13 +82,15 @@ 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 of the uploaded file.
samsonasik marked this conversation as resolved.
Show resolved Hide resolved
* @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, ?string $mimeType = null, ?int $size = null, ?int $error = null)
samsonasik marked this conversation as resolved.
Show resolved Hide resolved
{
$this->path = $path;
$this->clientPath = $clientPath;
samsonasik marked this conversation as resolved.
Show resolved Hide resolved
$this->name = $originalName;
$this->originalName = $originalName;
$this->originalMimeType = $mimeType;
Expand Down Expand Up @@ -267,6 +276,15 @@ public function getClientName(): string
return $this->originalName;
}

/**
* (PHP 8.1+)
* Returns the webkit relative path of the uploaded file on directory uploads.
*/
public function getClientPath(): string|null
samsonasik marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->clientPath;
}

/**
* Gets the temporary filename where the file was uploaded to.
*/
Expand Down
9 changes: 8 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 $clientPath The webkit relative path of the uploaded file.
* @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, ?string $mimeType = null, ?int $size = null, ?int $error = null);

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

/**
* (PHP 8.1+)
* Returns the webkit relative path of the uploaded file on directory uploads.
*/
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
12 changes: 7 additions & 5 deletions tests/system/HTTP/Files/FileCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ public function testAllReturnsValidSingleFile()
{
$_FILES = [
'userfile' => [
'name' => 'someFile.txt',
'type' => 'text/plain',
'size' => '124',
'tmp_name' => '/tmp/myTempFile.txt',
'error' => 0,
'name' => 'someFile.txt',
'type' => 'text/plain',
'size' => '124',
'tmp_name' => '/tmp/myTempFile.txt',
'full_path' => 'tmp/myTempFile.txt',
samsonasik marked this conversation as resolved.
Show resolved Hide resolved
'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());
samsonasik marked this conversation as resolved.
Show resolved Hide resolved
$this->assertSame(124, $file->getSize());
}

Expand Down
8 changes: 8 additions & 0 deletions user_guide_src/source/libraries/uploaded_files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,14 @@ version, use ``getMimeType()`` instead:

.. literalinclude:: uploaded_files/015.php

getClientPath()
---------------

JamminCoder marked this conversation as resolved.
Show resolved Hide resolved
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.
In PHP versions below 8.1, this returns ``null``

.. literalinclude:: uploaded_files/023.php

Moving Files
============

Expand Down
4 changes: 4 additions & 0 deletions user_guide_src/source/libraries/uploaded_files/023.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

$clientPath = $file->getClientPath();
echo $clientPath; // dir/file.txt, or dir/sub_dir/file.txt