diff --git a/src/Network/Request.php b/src/Network/Request.php index 9bca2b82643..e22edc6e80b 100644 --- a/src/Network/Request.php +++ b/src/Network/Request.php @@ -1691,6 +1691,22 @@ public function getAttributes() return $this->attributes + $emulated; } + /** + * Get the uploaded file from a dotted path. + * + * @param string $path The dot separated path to the file you want. + * @return null|Psr\Http\Message\UploadedFileInterface + */ + public function getUploadedFile($path) + { + $file = Hash::get($this->uploadedFiles, $path); + if (!$file instanceof UploadedFile) { + return null; + } + + return $file; + } + /** * Get the array of uploaded files from the request. * diff --git a/tests/TestCase/Network/RequestTest.php b/tests/TestCase/Network/RequestTest.php index 8fcd14f3550..781a24765de 100644 --- a/tests/TestCase/Network/RequestTest.php +++ b/tests/TestCase/Network/RequestTest.php @@ -550,6 +550,38 @@ public function testWithUploadedFiles() $this->assertSame(['picture' => $file], $new->getUploadedFiles()); } + /** + * Test getting a single file + * + * @return void + */ + public function testGetUploadedFile() + { + $file = new UploadedFile( + __FILE__, + 123, + UPLOAD_ERR_OK, + 'test.php', + 'text/plain' + ); + $request = new Request(); + $new = $request->withUploadedFiles(['picture' => $file]); + $this->assertNull($new->getUploadedFile('')); + $this->assertSame($file, $new->getUploadedFile('picture')); + + $new = $request->withUploadedFiles([ + 'pictures' => [ + [ + 'image' => $file + ] + ] + ]); + $this->assertNull($new->getUploadedFile('pictures')); + $this->assertNull($new->getUploadedFile('pictures.0')); + $this->assertNull($new->getUploadedFile('pictures.1')); + $this->assertSame($file, $new->getUploadedFile('pictures.0.image')); + } + /** * Test replacing files with an invalid file *