Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpFoundation] Add UploadedFile::getClientOriginalPath() to support directory uploads #52493

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

Merged
merged 1 commit into from
Dec 4, 2023
Merged
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
11 changes: 6 additions & 5 deletions src/Symfony/Component/Form/NativeRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class NativeRequestHandler implements RequestHandlerInterface
*/
private const FILE_KEYS = [
'error',
'full_path',
'name',
'size',
'tmp_name',
Expand Down Expand Up @@ -186,9 +187,7 @@ private static function fixPhpFilesArray(mixed $data): mixed
return $data;
}

// Remove extra key added by PHP 8.1.
unset($data['full_path']);
$keys = array_keys($data);
$keys = array_keys($data + ['full_path' => null]);
sort($keys);

if (self::FILE_KEYS !== $keys || !isset($data['name']) || !\is_array($data['name'])) {
Expand All @@ -207,7 +206,9 @@ private static function fixPhpFilesArray(mixed $data): mixed
'type' => $data['type'][$key],
'tmp_name' => $data['tmp_name'][$key],
'size' => $data['size'][$key],
]);
] + (isset($data['full_path'][$key]) ? [
'full_path' => $data['full_path'][$key],
] : []));
}

return $files;
Expand All @@ -219,7 +220,7 @@ private static function stripEmptyFiles(mixed $data): mixed
return $data;
}

$keys = array_keys($data);
$keys = array_keys($data + ['full_path' => null]);
sort($keys);

if (self::FILE_KEYS === $keys) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public function testFixBuggyFilesArray()
'name' => [
'field' => 'upload.txt',
],
'full_path' => [
'field' => 'path/to/file/upload.txt',
],
'type' => [
'field' => 'text/plain',
],
Expand All @@ -118,6 +121,7 @@ public function testFixBuggyFilesArray()
$this->assertTrue($form->isSubmitted());
$this->assertEquals([
'name' => 'upload.txt',
'full_path' => 'path/to/file/upload.txt',
'type' => 'text/plain',
'tmp_name' => 'owfdskjasdfsa',
'error' => \UPLOAD_ERR_OK,
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.1
---

* Add `UploadedFile::getClientOriginalPath()`

7.0
---

Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/HttpFoundation/File/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class UploadedFile extends File
private string $originalName;
private string $mimeType;
private int $error;
private string $originalPath;

/**
* Accepts the information of the uploaded file as provided by the PHP global $_FILES.
Expand Down Expand Up @@ -63,6 +64,7 @@ class UploadedFile extends File
public function __construct(string $path, string $originalName, string $mimeType = null, int $error = null, bool $test = false)
{
$this->originalName = $this->getName($originalName);
$this->originalPath = strtr($originalName, '\\', '/');
$this->mimeType = $mimeType ?: 'application/octet-stream';
$this->error = $error ?: \UPLOAD_ERR_OK;
$this->test = $test;
Expand Down Expand Up @@ -92,6 +94,21 @@ public function getClientOriginalExtension(): string
return pathinfo($this->originalName, \PATHINFO_EXTENSION);
}

/**
* Returns the original file full path.
*
* It is extracted from the request from which the file has been uploaded.
* This should not be considered as a safe value to use for a file name/path on your servers.
*
* If this file was uploaded with the "webkitdirectory" upload directive, this will contain
* the path of the file relative to the uploaded root directory. Otherwise this will be identical
* to getClientOriginalName().
*/
public function getClientOriginalPath(): string
{
return $this->originalPath;
}

/**
* Returns the file mime type.
*
Expand Down
22 changes: 11 additions & 11 deletions src/Symfony/Component/HttpFoundation/FileBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
class FileBag extends ParameterBag
{
private const FILE_KEYS = ['error', 'name', 'size', 'tmp_name', 'type'];
private const FILE_KEYS = ['error', 'full_path', 'name', 'size', 'tmp_name', 'type'];

/**
* @param array|UploadedFile[] $parameters An array of HTTP files
Expand Down Expand Up @@ -65,18 +65,18 @@ protected function convertFileInformation(array|UploadedFile $file): array|Uploa
}

$file = $this->fixPhpFilesArray($file);
$keys = array_keys($file);
$keys = array_keys($file + ['full_path' => null]);
sort($keys);

if (self::FILE_KEYS == $keys) {
if (\UPLOAD_ERR_NO_FILE == $file['error']) {
if (self::FILE_KEYS === $keys) {
if (\UPLOAD_ERR_NO_FILE === $file['error']) {
$file = null;
} else {
$file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['error'], false);
$file = new UploadedFile($file['tmp_name'], $file['full_path'] ?? $file['name'], $file['type'], $file['error'], false);
}
} else {
$file = array_map(fn ($v) => $v instanceof UploadedFile || \is_array($v) ? $this->convertFileInformation($v) : $v, $file);
if (array_keys($keys) === $keys) {
if (array_is_list($file)) {
$file = array_filter($file);
}
}
Expand All @@ -98,12 +98,10 @@ protected function convertFileInformation(array|UploadedFile $file): array|Uploa
*/
protected function fixPhpFilesArray(array $data): array
{
// Remove extra key added by PHP 8.1.
unset($data['full_path']);
$keys = array_keys($data);
$keys = array_keys($data + ['full_path' => null]);
sort($keys);

if (self::FILE_KEYS != $keys || !isset($data['name']) || !\is_array($data['name'])) {
if (self::FILE_KEYS !== $keys || !isset($data['name']) || !\is_array($data['name'])) {
return $data;
}

Expand All @@ -119,7 +117,9 @@ protected function fixPhpFilesArray(array $data): array
'type' => $data['type'][$key],
'tmp_name' => $data['tmp_name'][$key],
'size' => $data['size'][$key],
]);
] + (isset($data['full_path'][$key]) ? [
'full_path' => $data['full_path'][$key],
] : []));
}

return $files;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nested webkitdirectory text
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
webkitdirectory text
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,26 @@ public function testGetMaxFilesize()
$this->assertSame(\PHP_INT_MAX, $size);
}
}

public function testgetClientOriginalPath()
{
$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'test.gif',
'image/gif'
);

$this->assertEquals('test.gif', $file->getClientOriginalPath());
}

public function testgetClientOriginalPathWebkitDirectory()
{
$file = new UploadedFile(
__DIR__.'/Fixtures/webkitdirectory/test.txt',
'webkitdirectory/test.txt',
'text/plain',
);

$this->assertEquals('webkitdirectory/test.txt', $file->getClientOriginalPath());
}
}
76 changes: 53 additions & 23 deletions src/Symfony/Component/HttpFoundation/Tests/FileBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,12 @@ public function testFileMustBeAnArrayOrUploadedFile()
public function testShouldConvertsUploadedFiles()
{
$tmpFile = $this->createTempFile();
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain');
$name = basename($tmpFile);

$bag = new FileBag(['file' => [
'name' => basename($tmpFile),
'type' => 'text/plain',
'tmp_name' => $tmpFile,
'error' => 0,
'size' => null,
]]);

$this->assertEquals($file, $bag->get('file'));
}

public function testShouldConvertsUploadedFilesPhp81()
{
$tmpFile = $this->createTempFile();
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain');
$file = new UploadedFile($tmpFile, $name, 'text/plain');

$bag = new FileBag(['file' => [
'name' => basename($tmpFile),
'full_path' => basename($tmpFile),
'name' => $name,
'type' => 'text/plain',
'tmp_name' => $tmpFile,
'error' => 0,
Expand Down Expand Up @@ -104,12 +89,13 @@ public function testShouldNotRemoveEmptyUploadedFilesForAssociativeArray()
public function testShouldConvertUploadedFilesWithPhpBug()
{
$tmpFile = $this->createTempFile();
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain');
$name = basename($tmpFile);
$file = new UploadedFile($tmpFile, $name, 'text/plain');

$bag = new FileBag([
'child' => [
'name' => [
'file' => basename($tmpFile),
'file' => $name,
],
'type' => [
'file' => 'text/plain',
Expand All @@ -133,12 +119,13 @@ public function testShouldConvertUploadedFilesWithPhpBug()
public function testShouldConvertNestedUploadedFilesWithPhpBug()
{
$tmpFile = $this->createTempFile();
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain');
$name = basename($tmpFile);
$file = new UploadedFile($tmpFile, $name, 'text/plain');

$bag = new FileBag([
'child' => [
'name' => [
'sub' => ['file' => basename($tmpFile)],
'sub' => ['file' => $name],
],
'type' => [
'sub' => ['file' => 'text/plain'],
Expand All @@ -162,13 +149,56 @@ public function testShouldConvertNestedUploadedFilesWithPhpBug()
public function testShouldNotConvertNestedUploadedFiles()
{
$tmpFile = $this->createTempFile();
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain');
$name = basename($tmpFile);
$file = new UploadedFile($tmpFile, $name, 'text/plain');
$bag = new FileBag(['image' => ['file' => $file]]);

$files = $bag->all();
$this->assertEquals($file, $files['image']['file']);
}

public function testWebkitDirectoryUpload()
{
$file1 = __DIR__.'/File/Fixtures/webkitdirectory/test.txt';
$file2 = __DIR__.'/File/Fixtures/webkitdirectory/nested/test.txt';

$bag = new FileBag([
'child' => [
'name' => [
'test.txt',
'test.txt',
],
'full_path' => [
'webkitdirectory/test.txt',
'webkitdirectory/nested/test.txt',
],
'type' => [
'text/plain',
'text/plain',
],
'tmp_name' => [
$file1,
$file2,
],
'error' => [
0, 0,
],
'size' => [
null, null,
],
],
]);

/** @var UploadedFile[] */
$files = $bag->get('child');

$this->assertEquals('test.txt', $files[0]->getClientOriginalName());
$this->assertEquals('test.txt', $files[1]->getClientOriginalName());

$this->assertEquals('webkitdirectory/test.txt', $files[0]->getClientOriginalPath());
$this->assertEquals('webkitdirectory/nested/test.txt', $files[1]->getClientOriginalPath());
}

protected function createTempFile()
{
$tempFile = tempnam(sys_get_temp_dir().'/form_test', 'FormTest');
Expand Down